469,306 Members | 2,519 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

How to compare two Objects....

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...
Nov 21 '05 #1
7 12215
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
Nov 21 '05 #2
Peter,

"Prabhudhas Peter" <Pr*************@discussions.microsoft.com> schrieb:
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>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
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
"Prabhudhas Peter" <Pr*************@discussions.microsoft.com> wrote in
message news:14**********************************@microsof t.com...
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...

Nov 21 '05 #4
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

--------------------
Thread-Topic: How to compare two Objects....
thread-index: AcURdFdX63l6Wz8ZS/CxxQL6U3h/ig==
X-WBNR-Posting-Host: 61.247.248.163
From: "=?Utf-8?B?UHJhYmh1ZGhhcyBQZXRlcg==?=" <Pr*************@discussions.microsoft.com>
Subject: How to compare two Objects....
Date: Sat, 12 Feb 2005 18:33:02 -0800
Lines: 9
Message-ID: <14**********************************@microsoft.co m>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
Path: TK2MSFTNGXA01.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFT NGXA03.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.languages.vb:70678
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

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...

Nov 21 '05 #5
Dont forget about types
the is operator
"Alexandre Moura" <am****@online.microsoft.com> wrote in message
news:7q**************@TK2MSFTNGXA02.phx.gbl...
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

--------------------
Thread-Topic: How to compare two Objects....
thread-index: AcURdFdX63l6Wz8ZS/CxxQL6U3h/ig==
X-WBNR-Posting-Host: 61.247.248.163
From: "=?Utf-8?B?UHJhYmh1ZGhhcyBQZXRlcg==?="
<Pr*************@discussions.microsoft.com>
Subject: How to compare two Objects....
Date: Sat, 12 Feb 2005 18:33:02 -0800
Lines: 9
Message-ID: <14**********************************@microsoft.co m>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
Path: TK2MSFTNGXA01.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFT NGXA03.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.languages.vb:70678
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

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...


Nov 21 '05 #6
Sorry, I'm not sure I follow you - do you mean the typeof...is operator?

Alex

--------------------
Reply-To: "Chris Calzaretta" <CC*********@HOTMAIL.COM>
From: "Chris Calzaretta" <cc*********@hotmail.com>
References: <14**********************************@microsoft.co m> <7q**************@TK2MSFTNGXA02.phx.gbl>
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: <uC**************@TK2MSFTNGP12.phx.gbl>
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!TK2MSF TNGP08.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 Moura" <am****@online.microsoft.com> wrote in message
news:7q**************@TK2MSFTNGXA02.phx.gbl...
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

--------------------
Thread-Topic: How to compare two Objects....
thread-index: AcURdFdX63l6Wz8ZS/CxxQL6U3h/ig==
X-WBNR-Posting-Host: 61.247.248.163
From: "=?Utf-8?B?UHJhYmh1ZGhhcyBQZXRlcg==?="
<Pr*************@discussions.microsoft.com>
Subject: How to compare two Objects....
Date: Sat, 12 Feb 2005 18:33:02 -0800
Lines: 9
Message-ID: <14**********************************@microsoft.co m>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
Path: TK2MSFTNGXA01.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFT NGXA03.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.languages.vb:70678
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

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...



Nov 21 '05 #7
Alexandre,

"Alexandre Moura" <am****@online.microsoft.com> schrieb:
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.
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Helmut Jarausch | last post: by
4 posts views Thread by Nicolas Fleury | last post: by
3 posts views Thread by Stephen | last post: by
8 posts views Thread by laniik | last post: by
3 posts views Thread by vertigo | last post: by
19 posts views Thread by David zhu | last post: by
4 posts views Thread by Gaby | last post: by
4 posts views Thread by Lamis | last post: by
50 posts views Thread by titan nyquist | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.