473,756 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Equa ls(tempStr);
}

if (obj is foo)
{
foo tempfoo = (foo)obj;
return tempfoo.Name.Eq uals(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 3781

"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.Equa ls(tempStr);
}

if (obj is foo)
{
foo tempfoo = (foo)obj;
return tempfoo.Name.Eq uals(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**********@D oNotSpam.hotmai l.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.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
2223
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 bool Equals(object obj) {} Another is using the override keyword, like: public override bool Equals(object obj) {}
1
1933
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 overriding the GetHashCode base method (inherited from object) was a good idea. public abstract class CmsObjectNode : CmsObject, IXml { private Guid cmsObjectID = Guid.NewGuid();
12
6753
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 True. Therefore, overriding the Equals method in the class definition of the items I put in the ArrayList should make the IndexOf method use my version of the Equals method?!
18
4743
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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
8
3746
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 == and != opperators. Am I missing something or when would one want to have different implementations for Equals and ==? --Ken
10
105194
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 make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly. In this article I'll take a look at the equals and hashCode methods. ...
0
9456
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
9275
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
9872
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...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
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...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3805
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
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
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.