473,624 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12877
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.m icrosoft.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/frlrfsystemicom parableclasstop ic.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.GetPropert ies() and Type.GetPropert y()
method with the PropertyInfo class to evaluate the properties.

HTH

DalePres
MCAD, MCDBA, MCSE
"Prabhudhas Peter" <Pr************ *@discussions.m icrosoft.com> wrote in
message news:14******** *************** ***********@mic rosoft.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: AcURdFdX63l6Wz8 ZS/CxxQL6U3h/ig==
X-WBNR-Posting-Host: 61.247.248.163
From: "=?Utf-8?B?UHJhYmh1ZGh hcyBQZXRlcg==?= " <Pr************ *@discussions.m icrosoft.com>
Subject: How to compare two Objects....
Date: Sat, 12 Feb 2005 18:33:02 -0800
Lines: 9
Message-ID: <14************ *************** *******@microso ft.com>
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.publi c.dotnet.langua ges.vb
NNTP-Posting-Host: TK2MSFTNGXA03.p hx.gbl 10.40.1.29
Path: TK2MSFTNGXA01.p hx.gbl!cpmsftng xa06.phx.gbl!TK 2MSFTNGXA03.phx .gbl
Xref: TK2MSFTNGXA01.p hx.gbl microsoft.publi c.dotnet.langua ges.vb:70678
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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******** ******@TK2MSFTN GXA02.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: AcURdFdX63l6Wz8 ZS/CxxQL6U3h/ig==
X-WBNR-Posting-Host: 61.247.248.163
From: "=?Utf-8?B?UHJhYmh1ZGh hcyBQZXRlcg==?= "
<Pr********** ***@discussions .microsoft.com>
Subject: How to compare two Objects....
Date: Sat, 12 Feb 2005 18:33:02 -0800
Lines: 9
Message-ID: <14************ *************** *******@microso ft.com>
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.publi c.dotnet.langua ges.vb
NNTP-Posting-Host: TK2MSFTNGXA03.p hx.gbl 10.40.1.29
Path: TK2MSFTNGXA01.p hx.gbl!cpmsftng xa06.phx.gbl!TK 2MSFTNGXA03.phx .gbl
Xref: TK2MSFTNGXA01.p hx.gbl microsoft.publi c.dotnet.langua ges.vb:70678
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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*********@HO TMAIL.COM>
From: "Chris Calzaretta" <cc*********@ho tmail.com>
References: <14************ *************** *******@microso ft.com> <7q************ **@TK2MSFTNGXA0 2.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.publi c.dotnet.langua ges.vb
NNTP-Posting-Host: cpe-24-163-239-122.mn.rr.com 24.163.239.122
Path: TK2MSFTNGXA02.p hx.gbl!TK2MSFTN GXA01.phx.gbl!T K2MSFTNGP08.phx .gbl!TK2MSFTNGP 12.phx.gbl
Xref: TK2MSFTNGXA02.p hx.gbl microsoft.publi c.dotnet.langua ges.vb:261045
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vb

Dont forget about types
the is operator
"Alexandre Moura" <am****@online. microsoft.com> wrote in message
news:7q******* *******@TK2MSFT NGXA02.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: AcURdFdX63l6Wz8 ZS/CxxQL6U3h/ig==
X-WBNR-Posting-Host: 61.247.248.163
From: "=?Utf-8?B?UHJhYmh1ZGh hcyBQZXRlcg==?= "
<Pr********* ****@discussion s.microsoft.com >
Subject: How to compare two Objects....
Date: Sat, 12 Feb 2005 18:33:02 -0800
Lines: 9
Message-ID: <14************ *************** *******@microso ft.com>
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.publi c.dotnet.langua ges.vb
NNTP-Posting-Host: TK2MSFTNGXA03.p hx.gbl 10.40.1.29
Path: TK2MSFTNGXA01.p hx.gbl!cpmsftng xa06.phx.gbl!TK 2MSFTNGXA03.phx .gbl
Xref: TK2MSFTNGXA01.p hx.gbl microsoft.publi c.dotnet.langua ges.vb:70678
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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
1765
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 two examples if 2 > '1' : print "greater" else : print "less_or_equal"
4
5116
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 Regards, Nicolas
3
2071
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 has cascade set on one db and not on another, or unique on one and not the other, Constraints, Triggers, Stored procs, Users, Roles. I downloaded the SQL Data Compare 3.0 application 14 day trial and it seemed really good however does a bit more...
8
16593
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
3254
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
9506
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 have tried to print out by debug.writeline(). But the "==" operator results false, and string.Compare() results true. Somebody helps me!
4
7580
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
6658
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
20280
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
8236
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8173
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8679
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8621
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8475
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2606
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.