?
Solved

C#, override Equals() & operator==, list<obj>.contains will not return true.

Posted on 2013-01-03
2
Medium Priority
?
929 Views
Last Modified: 2013-01-03
I have a class where I have overridden "==" and "Equals()", which does work to return true when comparing two different IntVector2's with identical values.

However, I try using it as a Key in a dictionary, and it will not return anything, so I have to use the following code to get the object reference I want.
				IntVector2 iv = new IntVector2(x, y);
				foreach(IntVector2 iv2 in this.HeightMap.Keys)
				{
					if(iv2 == iv) 
					{
						iv = iv2;
						break;
					}
				}

Open in new window


What do I need to do so a dictionary can use a custom object as a key like this?

public class IntVector2
{
	public int X = 0;
	public int Y = 0;
	public IntVector2 (int x, int y)
	{
		this.X = x;
		this.Y = y;
	}

	public static bool operator ==(IntVector2 v1, IntVector2 v2)
	{
		return (v1.X == v2.X) && (v1.Y == v2.Y);	
	}
	public static bool operator !=(IntVector2 v1, IntVector2 v2)
	{
		return (v1.Y != v2.Y) || (v1.X != v2.X);	
	}
	public override bool Equals (object obj)
	{
		if(obj is IntVector2)
		{
			IntVector2 v1 = (IntVector2)obj;
			return (v1.X == this.X) && (v1.Y == this.Y);
		}
		return false;
	}
	
}

Open in new window

0
Comment
Question by:hpdvs2
  • 2
2 Comments
 
LVL 8

Accepted Solution

by:
hpdvs2 earned 0 total points
ID: 38742700
Got it, It appears that .Contains() depends on HashTag, so I had to override that as well, and try to create a number from it unique for each one, depending on the values.
0
 
LVL 8

Author Closing Comment

by:hpdvs2
ID: 38742701
Been bugin me, but I got it working.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question