How to compare two Objects....

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have two object instances of a same class...
and i assigned values in both object instances (or the values can be taken
from databse and assigned to the members of the objects)...
Now i want to compare these two objects so that it will return true if both
object's members have the same value...
it is good if u can give me a single function or simple code snippet..
Thank U
 
Peter,

If you only want to know if they are equal, you can serialize them in my
opinion and than compare. When you want to know what is different in the
individual objects inside your object, than is comparing object by object in
my opinion the only way.

I hope this helps?

Cor
 
Peter,

Prabhudhas Peter said:
I have two object instances of a same class...
and i assigned values in both object instances (or the values can be taken
from databse and assigned to the members of the objects)...
Now i want to compare these two objects so that it will return true if
both
object's members have the same value...


Implement the interface 'IComparable' in your class. This will add a method
'CompareTo' which can be used to compare the object to another object passed
to 'CompareTo'. You will have to implement comparison of property values
yourself:

..NET Framework Class Library -- 'IComparable' Interface
<URL:http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemicomparableclasstopic.asp>
 
If you want to do this generically, without necessarily knowing the classes
ahead of time, you can use the Type.GetProperties() and Type.GetProperty()
method with the PropertyInfo class to evaluate the properties.

HTH

DalePres
MCAD, MCDBA, MCSE
 
There isn't a direct way in vb to do that - the other replies on this thread point to valid alternatives for comparison, I just wanted to expand a bit more.
The main problem, in the end, is that different people have different ideas of what it means to say that two objects are equal - say that you have two
integers, and you want to compare them - then usually everyone considers that if they hold the same value, then they're equal.
Now, for user defined types - say you've got a class with only integer members - you may consider two instances are equal if all the members are equal -
most people would. So far, so good. But say that the class actually holds references to other classes, now what? should we compare that they point to the
same instance? cascade the comparison and verify that each of the members are equal? What happens if one of the members holds a handle identifier,
and we just happen to have two different handles that actually refer to the same object in someone's storage system?
We could have gone with saying that all members have to be binarily equal, but in the end the decision was left to the user - (btw, in 2005 you'll be able to
overload the = operator, allowing to state things like: "if mycls1var = mycls2var then ..."), so you end up deciding what it means to say that two classes are
equal - the downside is that you'll have to write specific code to do the comparison yourself.

Alex, MS VB QA

--------------------
 
Dont forget about types
the is operator
Alexandre Moura said:
There isn't a direct way in vb to do that - the other replies on this
thread point to valid alternatives for comparison, I just wanted to expand
a bit more.
The main problem, in the end, is that different people have different
ideas of what it means to say that two objects are equal - say that you
have two
integers, and you want to compare them - then usually everyone considers
that if they hold the same value, then they're equal.
Now, for user defined types - say you've got a class with only integer
members - you may consider two instances are equal if all the members are
equal -
most people would. So far, so good. But say that the class actually holds
references to other classes, now what? should we compare that they point
to the
same instance? cascade the comparison and verify that each of the members
are equal? What happens if one of the members holds a handle identifier,
and we just happen to have two different handles that actually refer to
the same object in someone's storage system?
We could have gone with saying that all members have to be binarily equal,
but in the end the decision was left to the user - (btw, in 2005 you'll be
able to
overload the = operator, allowing to state things like: "if mycls1var =
mycls2var then ..."), so you end up deciding what it means to say that two
classes are
equal - the downside is that you'll have to write specific code to do the
comparison yourself.

Alex, MS VB QA

--------------------
 
Sorry, I'm not sure I follow you - do you mean the typeof...is operator?

Alex

--------------------
Reply-To: "Chris Calzaretta" <[email protected]>
From: "Chris Calzaretta" <[email protected]>
References: <[email protected]> <[email protected]>
Subject: Re: How to compare two Objects....
Date: Fri, 18 Feb 2005 18:46:14 -0600
Lines: 73
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: cpe-24-163-239-122.mn.rr.com 24.163.239.122
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.languages.vb:261045
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Dont forget about types
the is operator
 
Alexandre,

Alexandre Moura said:
Sorry, I'm not sure I follow you - do you mean the typeof...is operator?

I assume that Chris is referring to the 'Is' operator which can be used to
check for reference equality:

\\\
Dim f1 As New Foo()
Dim f2 As Foo = f1
MsgBox(CStr(f1 Is f2) ' 'True' if 'f1' and 'f2' point to the same object.
///
 

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

Back
Top