Solved

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

Posted on 2013-01-03
2
907 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

717 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