- Home /
[C#] custom class keys in a dictionary, overloading == etc.
Hello :)
I made a custom class iVector2 which is just a Vector2 but with ints instead of floats. I know a bit about overloading operators, but just the super basic basics.
I'm using a dictionary, the keys of which are iVector2s. I got an error "key not present in dictionary" somewhere where I was sure it shouldn't appear. I assumed it was because I didn't overload an operator ==. But once I did this I got the following warnings:
`iVector2' defines operator == or operator != but does not override Object.Equals(object o)
`iVector2' defines operator == or operator != but does not override Object.GetHashCode()
Ok, so first of all I'd like to know if my reasoning was correct and this error with the keys could have been caused by this or not. I'd also like to know how to get rid of those warnings and what this Object.Equals is. Does it have something to do with interfaces like IComparable (which I know nothing about)?
Thank you very much, have a nice day.
Code:
public int x;
public int y;
public iVector2() {
x = 0;
y = 0;
}
public static iVector2 operator ==(iVector2 i1, iVector2 i2) {
return ((i1.x == iVector2.x) && (i1.y == i2.y));
}
Answer by fafase · Feb 23, 2015 at 06:16 PM
The following is more or less the standard way:
public override bool Equals(object o)
{
if (o is iVector2)
return Equals((iVector2)o);
else
return base.Equals(o);
}
public bool Equals(iVector2 vec)
{
if (vec == null)
return false;
if (this.x == vec.x && this.y == vec.y)
return true;
else
return false;
}
public static bool operator ==(iVector2 v1, iVector2 v2)
{
if (Object.ReferenceEquals(v1, v2))
{
return true;
}
if (v1 == null || v2 == null)
{
return false;
}
}
public static bool operator !=(iVector2 v1, iVector2 v2)
{
if (Object.ReferenceEquals(v1, v2))
{
return false;
}
if (v1 == null || v2 == null)
{
return true;
}
return !((v1.x == v2.x) && (v1.y == v2.y));
}
// this implementation is not necessary
// Only the override is
public override int GetHashCode()
{
int hash = 17;
// Suitable nullity checks etc, of course :)
hash = hash * 23 + x.GetHashCode();
hash = hash * 23 + y.GetHashCode();
return hash;
}
C# requires that if you override the equal sign then it wants some other override to secure that you would not misuse the opposite. This is how the language is made.
Now for your dictionary issue, you would have to show the code you use to fill the dictionary (where you do dict.Add(key, value);)
Oh boy, I'll have to learn something about all this and then I will return to this answer of yours, for which I thank you. But for now, could you, please, also answer this:
"I can see that the Vector2 that Unity provides stores floats and I wasn't sure if it wouldn't cause any problems if I decide to use Vector2's fields to store something that must be an int (like an index for an array etc.). Do I need to cast v.x to (int) everytime or not? Cheers"
Thanks.
If you think that the dictionary-keys error appears because I did something wrong with filling the dictionary, it's a simple enough problem for me to double-check the code and handle it myself. Thanks.
Consider this:
Vector2 vec = new Vector2(2.0001, 2.9999f);
int x = (int)vec.x;
int y = (int)vec.y;
issue here is that your vector is closer to (2, 3) but what you will get is (2, 2) since casting just remove the floating part.
Also, I am not sure you can actually rely on them since float are not exactly what they are, I mean when you think you have a 2.00, it is possible that it is stored as 1.9999999999997 due to the fact that float ing point value are stored as
x * 10^y
so it might not be exactly the value, just real close.
So there could be a chance that 2 is stored as 1.9999999999999 which in turn becomes 1 when cast to int. (Not sue though)
Now you can use use $$anonymous$$athf.Round to fix that issue, this would depend on what you are trying to achieve.
No, no, that's not the problem, cause I will only be storing ints in them and using them as ints. All I wanted to know was if the casting would be necessary every time, but you know what? I can check that myself, so don't bother. Thanks for help, have a nice life :)
floats can represent ints with accuracy. http://cottonvibes.blogspot.ca/2010/08/32bit-floats-integer-accuracy-and.html
@kaka24fan Casting shouldn't be a problem, but if you're using your class to represent array indices, that's O$$anonymous$$ too. I thought maybe you were using it for transform positioning, rotation, scale, etc.
Answer by Jessespike · Feb 23, 2015 at 05:50 PM
Should return a bool. Also don't need a static inside the function.
public static bool operator ==(iVector2 i1, iVector2 i2) {
return ((i1.x == i2.x) && (i1.y == i2.y));
}
http://www.dotnetperls.com/operator
If you're writing this code for practice, then that's fine. But if you're trying to optimize the current vectors or something for your project, I would recommend that you don't and just use the Unity vectors as-is.
I can see that the Vector2 that Unity provides stores floats and I wasn't sure if it wouldn't cause any problems if I decide to use Vector2's fields to store something that must be an int (like an index for an array etc.). Do I need to cast v.x to (int) everytime or not? Cheers