Question about equality

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
 
J

Joe Mayo

Matt Burland said:
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.

Hi Matt,

I would have to see the object implementations to tell for sure why the
difference between type A and type B. If a object is a value type, you will
have value equality. If the object is a reference type, the default
behavior is reference equality. To get value equality for a reference type,
you will have to implement Equals in your type (and == while your at it).

Also, look at how you are creating IdenticalCopyOfMyA and
IdenticalCopyOfMyB. Are both objects IClonable?Are the implementations
different? Is it possible that you are returning a reference to A, but
creating a new instance of B?

Joe
 
M

Madhu [MVP]

Hi,

Both the equality and the == operator works the same way
for classes. It only does reference equality rather than
value equality. To do value equality you need to override
equals.

Hope this helps...

Regards,
Madhu

MVP | MCSD.NET
 
1

100

Hi Matt,

Is the type of MyA a *struct* insted of *class*? The default implementation
for classes (reference types) is to compare the references (addresses) only.
For value types (struct and enum) the default implementation uses reflection
to compare the fields of the objects.

So, if you have two identical copies of an object (two different objects
with identical fields) obj1 and obj2
obj1 == obj2 will return:
*false* if the objects are instances of reference type (class)
*true* if the objects are instances of value type (struct or enum)

HTH
B\rgds
100
 
M

Matt Burland

Also, look at how you are creating IdenticalCopyOfMyA and
IdenticalCopyOfMyB. Are both objects IClonable?Are the implementations
different? Is it possible that you are returning a reference to A, but
creating a new instance of B?

Thanks for the reply, now that I look through the whole thing again I think
my copy of A was in fact a reference to A and not a separate A, while in B I
was looking at a separate object of class B. Makes sense now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top