Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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