How to check wo object are equal?

G

Guest

Dear all,

What is the proper way to check if two object are equal ?
I do not mean equal on Object type only but also object value's

thnaks for help
regards
serge
 
M

Michael Nemtsev

Hello serge,

Realize IComparable interface where in .CompareTo() implement members comparing


sc> Dear all,
sc>
sc> What is the proper way to check if two object are equal ? I do not
sc> mean equal on Object type only but also object value's
sc>
sc> thnaks for help
sc> regards
sc> serge
---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 
J

John Bailo

serge said:
Dear all,

What is the proper way to check if two object are equal ?
I do not mean equal on Object type only but also object value's

thnaks for help
regards
serge

Use the keyword *is*
 
M

Mattias Sjögren

What is the proper way to check if two object are equal ?
I do not mean equal on Object type only but also object value's

If the type overrides the Object.Equals method (and it should if it
supports value comparison), you can call that.

If it also overloads the equality operator (== in C#) you can also
consider using that.


Mattias
 
G

Guest

Hi michael,

Do you mean that my class need to implement the IComparabel interface ?
SOmething like this :

class my object
implements Icomparable

end class

How to use it then if I need to compare. Fore instance here is 2 objects

myObject1.Id=1
myObject12.Param= m_paramList_1
myObject1.Name="Object1"

myObject2.Id=2
myObject2.Param= m_paramList_2
myObject2.Name="Object2"

regard
serge
 
M

Michael Nemtsev

Hello serge,

sc> Hi michael,
sc>
sc> Do you mean that my class need to implement the IComparabel
sc> interface ? SOmething like this :

with CompareTo method only
see MSDN sample

sc> How to use it then if I need to compare. Fore instance here is 2
sc> objects
sc>
sc> myObject1.Id=1
sc> myObject12.Param= m_paramList_1
sc> myObject1.Name="Object1"
sc> myObject2.Id=2
sc> myObject2.Param= m_paramList_2
sc> myObject2.Name="Object2"

Just myObject1.CompareTo(myObject2)


sc> "Michael Nemtsev said:
Hello serge,

Realize IComparable interface where in .CompareTo() implement members
comparing

sc> Dear all,
sc>
sc> What is the proper way to check if two object are equal ? I do
not
sc> mean equal on Object type only but also object value's
sc>
sc> thnaks for help
sc> regards
sc> serge
---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour
"At times one remains faithful to a cause only because its opponents
do not cease to be insipid." (c) Friedrich Nietzsche
---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
J

John Bailo

Mattias said:
If the type overrides the Object.Equals method (and it should if it
supports value comparison), you can call that.

If it also overloads the equality operator (== in C#) you can also
consider using that.


Mattias

I don't see any short cut method of doing the comparison, once you
establish the object types are equal with an "is" comparison.

At that point, unless you have some prior knowledge of the data
structure of the objects -- and I think the OP is implying that you
don't -- I think you would have to use Reflection to enumerate all the
members of the object.

Then, based on the type of each member, you would then have to do a
comparison. [ For example, take two ADODB record sets. Is there a
simple way to ask whether one completely equals the other? ]

And, objects can have other objects as members which can have their own
members that can be values, references, other objects and so on.
 
B

Ben Voigt

John Bailo said:
Mattias said:
If the type overrides the Object.Equals method (and it should if it
supports value comparison), you can call that.

If it also overloads the equality operator (== in C#) you can also
consider using that.


Mattias

I don't see any short cut method of doing the comparison, once you
establish the object types are equal with an "is" comparison.

At that point, unless you have some prior knowledge of the data structure
of the objects -- and I think the OP is implying that you don't -- I think
you would have to use Reflection to enumerate all the members of the
object.

Then, based on the type of each member, you would then have to do a
comparison. [ For example, take two ADODB record sets. Is there a
simple way to ask whether one completely equals the other? ]

And, objects can have other objects as members which can have their own
members that can be values, references, other objects and so on.

Which just proves you *need* prior knowledge of the objects' semantics.
Should a circular linked list of three identical elements compare equal to a
circular linked list with only one element (identical to each of the first
three)? If you follow references, you'd find equality at every stage, with
unlimited recursion... if you build a table of equality classes, your test
will terminate, but you still have a meaningless result.
 
J

Jon Skeet [C# MVP]

John Bailo said:
Use the keyword *is*

That only checks if an object is an instance of a type (where the type
is known at compile-time). What the OP is after is the Equals method.
 
J

John Bailo

Jon said:
That only checks if an object is an instance of a type (where the type
is known at compile-time). What the OP is after is the Equals method.

Yes, that looks like it.

But my question is: how "equal" is "equal".

For example, suppose that the objects are two arrays of other objects.

Will .Equals() return true only if the entire array is absolutely equal
down to the last member value of every object in the array? What if
the arrays have references to other objects...will it then evaluate
those other objects to determine if they are equal? Or would both
references have to point to the same object?
 
J

Jon Skeet [C# MVP]

John Bailo said:
Yes, that looks like it.

But my question is: how "equal" is "equal".

For example, suppose that the objects are two arrays of other objects.

Will .Equals() return true only if the entire array is absolutely equal
down to the last member value of every object in the array? What if
the arrays have references to other objects...will it then evaluate
those other objects to determine if they are equal? Or would both
references have to point to the same object?

That's up to the type. Arrays just have the idea of reference equality,
contrary to what you might expect. That's the default - effectively,
object.Equals only provides *identity* equality unless it's overridden.
 
J

Jon Skeet [C# MVP]

serge calderara said:
Do you mean that my class need to implement the IComparabel interface ?

No - at the simplest level, you just need to override Object.Equals.
 
J

John A. Bailo

Jon Skeet [C# MVP] wrote:

That's up to the type. Arrays just have the idea of reference equality,
contrary to what you might expect. That's the default - effectively,
object.Equals only provides *identity* equality unless it's overridden.

Ok, let me simplify.

Two objects.

Each has a reference to an object as a member.

The objects are "equal" but not the same.

Does .Equals() return true for the master object?
 
J

Jon Skeet [C# MVP]

John A. Bailo said:
Ok, let me simplify.

Two objects.

Each has a reference to an object as a member.

The objects are "equal" but not the same.

Does .Equals() return true for the master object?

By default, if you've got two objects (rather than two references to a
single object), Equals will return false. You need to override Equals
in order to get "value" semantics.

(Note that there's a significant exception here: value types (structs)
have a default implementation of Equals which does a shallow
comparison, i.e. it compares the values of members for identity.)
 
J

John Bailo

Jon said:
By default, if you've got two objects (rather than two references to a
single object), Equals will return false. You need to override Equals
in order to get "value" semantics.

(Note that there's a significant exception here: value types (structs)
have a default implementation of Equals which does a shallow
comparison, i.e. it compares the values of members for identity.)

Man, you are one smart S.O.B. !

Thanks for the education :D !
 

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