473,395 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 12834
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Helmut Jarausch | last post by:
Hi, what does Python do if two objects aren't comparable (to my opinion) If I've understood "Python in a Nutschell" correctly it should raise an exception but it doesn't do for me. Here are...
4
by: Nicolas Fleury | last post by:
Hi everyone, Is there a way to compare recursively two objects (compare their members recursively)? I'm only interested in equality or non-equality (no need for lower-than...). Thx and...
3
by: Stephen | last post by:
I have to write a .Net application which can compare SQL Databases including things like: - DB structure, PK's, FK's, indexes and types of indexes i.e. should be able to detect if the same index...
8
by: laniik | last post by:
Hi. I have a problem using STL's built in sort that seems impossible to get around. if i have: -------------------------------- struct object { int val; }
3
by: vertigo | last post by:
Hello I have std::map object and i want to have randomly sorted objects in it. I tried to: std::map<int,RandomCompare> myobject; and: struct RandomCompare{ bool operator(int i1, int i2){
19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
4
by: Gaby | last post by:
Hi all, What is the best way to compare 2 (large) ArrayLists filled with an object. Can you please help me? Gaby
4
by: Lamis | last post by:
Hi, what is the best way to compare 2 haschtables contatining objects. the objects has 2 property, name & value. I need to print out the differences -- LZ
50
by: titan nyquist | last post by:
I wish to compare two structs via == but it does not compile. I can overload and create my own == but am I missing something that c# already has implemented? ~titan
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.