Is this suppose to work like this:
I have a class called foo in which I tried to override equals and return
different answers based on what was passed in.
public class foo
{
private string _name;
public string Name
{
get
{
return _name;
}
}
public override bool Equals(object obj)
{
if (obj is string)
{
string tempStr = (string)obj;
return this._name.Equals(tempStr);
}
if (obj is foo)
{
foo tempfoo = (foo)obj;
return tempfoo.Name.Equals(this._name);
}
return false;
}
}
if I have an arraylist of foo objects and call the Contains method with a
string object as the parameter, the Equals method on the foo class is never
called. If I pass it a foo object, it all works as planned. Is there some
rule that say the parameter for the Equals method must be the same type as
the class Equals is based on?
Just curious.
Thanks
--
Frank Wisniewski MCSE 4.0, MCP+I, A+
f p w 2 3 @ h o t m a i l . c o m 3 3743
"Frank Wisniewski" wrote... if I have an arraylist of foo objects and call the Contains method with a string object as the parameter, the Equals method on the foo class is never called. If I pass it a foo object, it all works as planned. Is there some rule that say the parameter for the Equals method must be the same type as the class Equals is based on?
Nope, but as you use a string object as the argument, it's the string.Equals
that will be used.
The other way around would work, with an ArrayList of strings, where you
could pass a foo instance.
// Bjorn A
Frank,
In addition to Bjorn's comments. Is there some rule that say the parameter for the Equals method must be the same type as the class Equals is based on?
The "Design Guidelines for Class Library Developers -- Implementing the
Equals Method" http://msdn.microsoft.com/library/de...pconEquals.asp
States "x.Equals(y) returns the same value as y.Equals(x)"...
Hope this helps
Jay
"Frank Wisniewski" <fp***@hotmail.com> wrote in message
news:41**********@Usenet.com... Is this suppose to work like this:
I have a class called foo in which I tried to override equals and return different answers based on what was passed in.
public class foo { private string _name;
public string Name { get { return _name; } }
public override bool Equals(object obj) { if (obj is string) { string tempStr = (string)obj; return this._name.Equals(tempStr); }
if (obj is foo) { foo tempfoo = (foo)obj; return tempfoo.Name.Equals(this._name); }
return false; }
}
if I have an arraylist of foo objects and call the Contains method with a string object as the parameter, the Equals method on the foo class is never called. If I pass it a foo object, it all works as planned. Is there some rule that say the parameter for the Equals method must be the same type as the class Equals is based on?
Just curious.
Thanks -- Frank Wisniewski MCSE 4.0, MCP+I, A+ f p w 2 3 @ h o t m a i l . c o m
Oh,
Thanks Bjorn
that makes sense now.
--
Frank Wisniewski MCSE 4.0, MCP+I, A+
f p w 2 3 @ h o t m a i l . c o m
"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... "Frank Wisniewski" wrote...
if I have an arraylist of foo objects and call the Contains method with a string object as the parameter, the Equals method on the foo class is never called. If I pass it a foo object, it all works as planned. Is there some rule that say the parameter for the Equals method must be the same type as the class Equals is based on? Nope, but as you use a string object as the argument, it's the
string.Equals that will be used.
The other way around would work, with an ArrayList of strings, where you could pass a foo instance.
// Bjorn A This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Rafael Veronezi |
last post by:
I have some questions about override in inheritance, and virtual members.
I know that you can you override a method by two ways in C#, one, is
overriding with the new keyword, like:
public new...
|
by: Anders Borum |
last post by:
Hello!
I have a framework where all objects are uniquely identified by a GUID
(Global Unique Identifier). The objects are used in conjunction with lots of
hashtables and I was thinking, that...
|
by: Rubbrecht Philippe |
last post by:
Hi there,
According to documentation I read the ArrayList.IndexOf method uses the
Object.Equals method to loop through the items in its list and locate the
first index of an item that returns...
|
by: JohnR |
last post by:
From reading the documentation, this should be a relatively easy thing. I
have an arraylist of custom class instances which I want to search with
an"indexof" where I'm passing an instance if the...
|
by: Kenneth Baltrinic |
last post by:
When one overrides the Equals() method of an object, one is supposed to
override GetHashCode() as well and this makes good sense. But I have seen
lots of people who do this and do not override the...
|
by: r035198x |
last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize.
These were designed to be overridden according to specific general contracts. Other classes that...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |