M
Matt Burland
I'm a little confused about the way the default equality operator works with
classes. Here's the situation, I have two comboboxes that are each filled
with different object (i.e. ComboBox1 contains objects of class A, ComboBox2
contains objects of class B). What I'm trying to do is determine if a given
object is contained in one of the comboboxes, i.e.:
Combobox1.Items.Contains(MyA);
Combobox2.Items.Contains(MyB);
Now the problem is that this works fine for class A, but not for class B. In
class B two clearly identical (to me) objects are failing to return true
when I test for equality, i.e:
MyA == IdenticalCopyOfMyA; // true
MyB == IdenticalCopyOfMyB; // false
This is despite the fact that all the fields appear to be identical. So what
is actually being compared in the default Equals method and the equality
operator. I thought it performed a simple comparison of each field unless it
was overridden, but that doesn't seem to be the case here.
I can fix my particular problem here by overriding Equals and == in class B,
but I'd like to understand why this is happening.
Cheers
Matt
classes. Here's the situation, I have two comboboxes that are each filled
with different object (i.e. ComboBox1 contains objects of class A, ComboBox2
contains objects of class B). What I'm trying to do is determine if a given
object is contained in one of the comboboxes, i.e.:
Combobox1.Items.Contains(MyA);
Combobox2.Items.Contains(MyB);
Now the problem is that this works fine for class A, but not for class B. In
class B two clearly identical (to me) objects are failing to return true
when I test for equality, i.e:
MyA == IdenticalCopyOfMyA; // true
MyB == IdenticalCopyOfMyB; // false
This is despite the fact that all the fields appear to be identical. So what
is actually being compared in the default Equals method and the equality
operator. I thought it performed a simple comparison of each field unless it
was overridden, but that doesn't seem to be the case here.
I can fix my particular problem here by overriding Equals and == in class B,
but I'd like to understand why this is happening.
Cheers
Matt