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 == IdenticalCopyOfMyA; // true
MyB == IdenticalCopyOfMyB; // 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 4 1651
"Matt Burland" <an*******@discussions.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.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 == IdenticalCopyOfMyA; // true MyB == IdenticalCopyOfMyB; // 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 IdenticalCopyOfMyA and
IdenticalCopyOfMyB. 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
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.Items.Contains(MyA); Combobox2.Items.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 == IdenticalCopyOfMyA; // true MyB == IdenticalCopyOfMyB; // 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
.
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*******@discussions.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.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 == IdenticalCopyOfMyA; // true MyB == IdenticalCopyOfMyB; // 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
> Also, look at how you are creating IdenticalCopyOfMyA and IdenticalCopyOfMyB. 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |