hello..i've made a structure given below and the objects of that struct are stored in an arraylist

struct node
    {
        public int x;
        public int y;
        public int cost;
        public object parent;
    }

now when i'm adding a new object this type..i want to check if the arraylist already contains the object with the same values..the check should only focus on x,y and cost variables of the object leaving behind the parent variable..how do i achieve this??? which methods of the arraylist should i override?? any help would be appreciated

you will need to override equals in the struct

struct node
{
    public int x;
    public int y;
    public int cost;
    public object parent;
    public override bool Equals(object obj)
    {
          bool res = false;
          if (obj.GetType()== typeof(node))
          {
              node objCasted = (node)obj;
              res=objCasted.x == x && objCasted.y == y;
          }
          return res;
    }
}

test:

ArrayList ar = new ArrayList();
ar.Add(new node() { x=1, y=2});
if (ar.Contains(new node() { x = 1, y = 2 }))
    Console.WriteLine("Yes");
else
    Console.WriteLine("No");

the compiler shows a warning about overriding Object.GetHashCode() method tooo..how do i do this??? could you please explain

i did this and it works

public override int GetHashCode()
        {
            return base.GetHashCode();
        }

thanks a lot for help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.