Solved

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

Posted on 2013-01-03
2
896 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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