473,770 Members | 6,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about equality

I'm a little confused about the way the default equality operator works with
classes. Here's the situation, I have two comboboxes that are each filled
with different object (i.e. ComboBox1 contains objects of class A, ComboBox2
contains objects of class B). What I'm trying to do is determine if a given
object is contained in one of the comboboxes, i.e.:

Combobox1.Items .Contains(MyA);
Combobox2.Items .Contains(MyB);

Now the problem is that this works fine for class A, but not for class B. In
class B two clearly identical (to me) objects are failing to return true
when I test for equality, i.e:

MyA == IdenticalCopyOf MyA; // true
MyB == IdenticalCopyOf MyB; // false

This is despite the fact that all the fields appear to be identical. So what
is actually being compared in the default Equals method and the equality
operator. I thought it performed a simple comparison of each field unless it
was overridden, but that doesn't seem to be the case here.
I can fix my particular problem here by overriding Equals and == in class B,
but I'd like to understand why this is happening.

Cheers

Matt
Nov 15 '05 #1
4 1675


"Matt Burland" <an*******@disc ussions.microso ft.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
I'm a little confused about the way the default equality operator works with classes. Here's the situation, I have two comboboxes that are each filled
with different object (i.e. ComboBox1 contains objects of class A, ComboBox2 contains objects of class B). What I'm trying to do is determine if a given object is contained in one of the comboboxes, i.e.:

Combobox1.Items .Contains(MyA);
Combobox2.Items .Contains(MyB);

Now the problem is that this works fine for class A, but not for class B. In class B two clearly identical (to me) objects are failing to return true
when I test for equality, i.e:

MyA == IdenticalCopyOf MyA; // true
MyB == IdenticalCopyOf MyB; // false

This is despite the fact that all the fields appear to be identical. So what is actually being compared in the default Equals method and the equality
operator. I thought it performed a simple comparison of each field unless it was overridden, but that doesn't seem to be the case here.
I can fix my particular problem here by overriding Equals and == in class B, but I'd like to understand why this is happening.


Hi Matt,

I would have to see the object implementations to tell for sure why the
difference between type A and type B. If a object is a value type, you will
have value equality. If the object is a reference type, the default
behavior is reference equality. To get value equality for a reference type,
you will have to implement Equals in your type (and == while your at it).

Also, look at how you are creating IdenticalCopyOf MyA and
IdenticalCopyOf MyB. Are both objects IClonable?Are the implementations
different? Is it possible that you are returning a reference to A, but
creating a new instance of B?

Joe
--
http://www.csharp-station.com
Nov 15 '05 #2
Hi,

Both the equality and the == operator works the same way
for classes. It only does reference equality rather than
value equality. To do value equality you need to override
equals.

Hope this helps...

Regards,
Madhu

MVP | MCSD.NET
-----Original Message-----
I'm a little confused about the way the default equality operator works withclasses. Here's the situation, I have two comboboxes that are each filledwith different object (i.e. ComboBox1 contains objects of class A, ComboBox2contains objects of class B). What I'm trying to do is determine if a givenobject is contained in one of the comboboxes, i.e.:

Combobox1.Item s.Contains(MyA) ;
Combobox2.Item s.Contains(MyB) ;

Now the problem is that this works fine for class A, but not for class B. Inclass B two clearly identical (to me) objects are failing to return truewhen I test for equality, i.e:

MyA == IdenticalCopyOf MyA; // true
MyB == IdenticalCopyOf MyB; // false

This is despite the fact that all the fields appear to be identical. So whatis actually being compared in the default Equals method and the equalityoperator. I thought it performed a simple comparison of each field unless itwas overridden, but that doesn't seem to be the case here.I can fix my particular problem here by overriding Equals and == in class B,but I'd like to understand why this is happening.

Cheers

Matt
.

Nov 15 '05 #3
100
Hi Matt,

Is the type of MyA a *struct* insted of *class*? The default implementation
for classes (reference types) is to compare the references (addresses) only.
For value types (struct and enum) the default implementation uses reflection
to compare the fields of the objects.

So, if you have two identical copies of an object (two different objects
with identical fields) obj1 and obj2
obj1 == obj2 will return:
*false* if the objects are instances of reference type (class)
*true* if the objects are instances of value type (struct or enum)

HTH
B\rgds
100

"Matt Burland" <an*******@disc ussions.microso ft.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
I'm a little confused about the way the default equality operator works with classes. Here's the situation, I have two comboboxes that are each filled
with different object (i.e. ComboBox1 contains objects of class A, ComboBox2 contains objects of class B). What I'm trying to do is determine if a given object is contained in one of the comboboxes, i.e.:

Combobox1.Items .Contains(MyA);
Combobox2.Items .Contains(MyB);

Now the problem is that this works fine for class A, but not for class B. In class B two clearly identical (to me) objects are failing to return true
when I test for equality, i.e:

MyA == IdenticalCopyOf MyA; // true
MyB == IdenticalCopyOf MyB; // false

This is despite the fact that all the fields appear to be identical. So what is actually being compared in the default Equals method and the equality
operator. I thought it performed a simple comparison of each field unless it was overridden, but that doesn't seem to be the case here.
I can fix my particular problem here by overriding Equals and == in class B, but I'd like to understand why this is happening.

Cheers

Matt

Nov 15 '05 #4
> Also, look at how you are creating IdenticalCopyOf MyA and
IdenticalCopyOf MyB. Are both objects IClonable?Are the implementations
different? Is it possible that you are returning a reference to A, but
creating a new instance of B?


Thanks for the reply, now that I look through the whole thing again I think
my copy of A was in fact a reference to A and not a separate A, while in B I
was looking at a separate object of class B. Makes sense now.
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
3691
by: Will McGugan | last post by:
Hi, Why is that a tuple doesnt have the methods 'count' and 'index'? It seems they could be present on a immutable object. I realise its easy enough to convert the tuple to a list and do this, I'm just curious why it is neccesary.. Thanks,
3
1198
by: Lee Garrington | last post by:
Hi, I want to create a set of POINT's but I need to define the sorting and equality conditions for that. How do I tell the set container that I want them ordered ascendingly by point.x primarily and then by point.y secondarily? How do I tell it that 2 points are equal only when both point.x and point.y are equal?
8
4171
by: xixi | last post by:
when i create a join view like this create view JV104FZ.APJTINM1 (APAM32, APNO20, APQY05, PONO01, PONO05, PONO19, POCD01, POCD13, systimestamp, loginname, id ) as select JV104FZ.APPTINM.APAM32, JV104FZ.APPTINM.APNO20, JV104FZ.APPTINM.APQY05, JV104FZ.APPTINM.PONO01, JV104FZ.APPTINM.PONO05, JV104FZ.APPTINM.PONO19, COALESCE(JV104FZ.POPTOL.POCD01, ' '), COALESCE(JV104FZ.POPTOL.POCD13, ' '), JV104FZ.POPTOL.systimestamp,...
40
5730
by: Ike Naar | last post by:
In K&R "The C++ programming language (2nd ANSI C edition), the reference manual states (paragraphs 7.9 and 7.10) that pointer comparison is undefined for pointers that do not point to the same object. So if we have const char * foo = "foo" , * bar = "bar" ; int foobar = ( foo == bar ) ; would it mean that foobar is undefined?
4
2279
by: Robert W. | last post by:
I've built a complex collection object from "System.Collections.CollectionBase". With regard to it, I have a question about the "Contains" method. At first I thought that "Contains" would actually search the entire collection to tell me whether an object in the collection were identical with the comparison object I was passing it. But I opened up the same collection data into two separate collections (loaded them from disk) and did...
37
2818
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current behaviour, which is returning a value which is, really, id({}) < id(). He also said that default equality comparison will continue to be identity-based. This means that x == y will never raise an exception, as is the situation is now. Here's his reason:
19
1922
by: Mr.Rech | last post by:
Hi all, I've read some thread about isinstance(), why it is considered harmful and how you can achieve the same results using a coding style that doesn't break polymorphism etc... Since I'm trying to improve my Python knowledge, and I'm going to design a class hierarchy from scratch, I'd like to have some feedback about this from those of you much more experienced than me in OOP. Suppose I'm writing a base class with an __eq__ special...
6
1813
by: Brett Wesoloski | last post by:
I have a collection that I add my objects to. When I go to remove an object from the collect I get an error {"Cannot remove the specified item because it was not found in the specified Collection."} Now I do understand what it is saying but I know the object is in the collection. Here is some code.
6
4035
by: dunleav1 | last post by:
I have an application that uses the old join syntax instead of the SQL92 standards join syntax. I need to justify changing the code to the new standard. Is there any performance issue related to using the old syntax that are documented? Are there any other issues that I use to justify a code upgrade?
0
9591
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
9425
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
10228
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
10057
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
9869
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
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
3970
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
2
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.