Help me get this straight.

T

test account

I have an interface called IStatus.
I have 2 classes, PurchaseOrder and Invoice. They each have an accessor
named Status that return an object of type IStatus.

I have 4 classes that implement IStatus... InvoiceNote, InvoiceStatus,
PurchaseOrderStatus and PurchaseOrderNote.

Here's the problem, I have 2 PurchaseOrder objects, po1 and po2.
When I do
bool val = (po1.Status == po2.Status).
it seems to do a reference check instead of use the == operator, or the
Equals method of the po1.Status.
I've overriden the Equals method and the == and != operator in all of the
classes. My understanding is, it would use the == operator of the object
returned by po1.Status.
 
N

Nicholas Paldino [.NET/C# MVP]

You might have overridden the == operator and the Equals method (did you
override GetHashCode as well), but I don't think that is a good idea. Those
operators are only good when comparing references of the types that they are
declared on. However, you are probably comparing the interface references,
in which case the overloaded operators will not be applied.

I think that you should have your objects implement the IComparable
interface as well, or have your IStatus interface derive from IComparable
and use the CompareTo method for your comparisons.

Hope this helps.
 
T

test account

"However, you are probably comparing the interface references, in which case
the overloaded operators will not be applied."
Bingo!

Thx
I could do that too, but I use that for sorting and not all of my classes
implement IComparable. All my classes override both the equality operators
and the equals operator, I didn't overide the gethashcode even though the
compiler complains about it. What I will do though is use the Equals method.
Nicholas Paldino said:
You might have overridden the == operator and the Equals method (did you
override GetHashCode as well), but I don't think that is a good idea. Those
operators are only good when comparing references of the types that they are
declared on. However, you are probably comparing the interface references,
in which case the overloaded operators will not be applied.

I think that you should have your objects implement the IComparable
interface as well, or have your IStatus interface derive from IComparable
and use the CompareTo method for your comparisons.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

test account said:
I have an interface called IStatus.
I have 2 classes, PurchaseOrder and Invoice. They each have an accessor
named Status that return an object of type IStatus.

I have 4 classes that implement IStatus... InvoiceNote, InvoiceStatus,
PurchaseOrderStatus and PurchaseOrderNote.

Here's the problem, I have 2 PurchaseOrder objects, po1 and po2.
When I do
bool val = (po1.Status == po2.Status).
it seems to do a reference check instead of use the == operator, or the
Equals method of the po1.Status.
I've overriden the Equals method and the == and != operator in all of the
classes. My understanding is, it would use the == operator of the object
returned by po1.Status.
 

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