Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help me get this straight.

0 views
Skip to first unread message

test account

unread,
Jan 13, 2004, 3:40:44 PM1/13/04
to
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.


Nicholas Paldino [.NET/C# MVP]

unread,
Jan 13, 2004, 3:53:58 PM1/13/04
to
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]
- m...@spam.guard.caspershouse.com

"test account" <bitcha...@hax0r.dyndns.org> wrote in message
news:OAVeyVh2...@TK2MSFTNGP12.phx.gbl...

test account

unread,
Jan 13, 2004, 4:03:55 PM1/13/04
to
"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 [.NET/C# MVP]" <m...@spam.guard.caspershouse.com> wrote in
message news:%23ihXzeh...@TK2MSFTNGP10.phx.gbl...

0 new messages