Solved

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

Posted on 2013-01-03
2
889 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 run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now