473,395 Members | 1,774 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.

Overriding Equals

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
Nov 16 '05 #1
3 3750

"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


Nov 16 '05 #2
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

Nov 16 '05 #3
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

Nov 16 '05 #4

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

Similar topics

4
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...
1
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...
12
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...
18
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...
8
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...
10
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...
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:
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
0
BarryA
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...
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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,...

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.