473,413 Members | 1,807 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,413 software developers and data experts.

ArrayList Contains

C# VS2005

I have a class with 5 properties

I am adding instances this class to an ArrayList

I want to test to see if an instance is already in the ArrayList before
attempting to add the item

From my reading, it seems that I have to have an Equals method in my class

I have created an Equals method, but it is never called

Does anyone have some insight into my problem?

TIA

Harvey Sather
Saskatoon Canada
May 8 '06 #1
8 9501
"HarvSather" <ha********@msn.com> wrote in message
news:uH**************@TK2MSFTNGP02.phx.gbl...
C# VS2005

I have a class with 5 properties

I am adding instances this class to an ArrayList

I want to test to see if an instance is already in the ArrayList before
attempting to add the item

From my reading, it seems that I have to have an Equals method in my class
You don't have to have an equals method. It will match on the instance if
you don't. You only need the equals if you want something more advanced.
I have created an Equals method, but it is never called


Did you use the override keyword? If you don't use this it will not be
called.

Michael
May 8 '06 #2
Hello HarvSather,

You need compare classes' member not the classes instances.
H> C# VS2005
H> I have a class with 5 properties
H> I am adding instances this class to an ArrayList
H> I want to test to see if an instance is already in the ArrayList
H> before attempting to add the item
H> From my reading, it seems that I have to have an Equals method in my
H> class
H> I have created an Equals method, but it is never called
H> Does anyone have some insight into my problem?

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
May 8 '06 #3
In the Equals method, I compare the properties of two instances.
I want to know if one instance is the same as another.
The thing that tells me that is if one of the properties in each instance is
equal.

The thing is, the equals method never gets called.

So, I am not sure if we are talking about the same thing.

Do you have some sample "working" code that you can post?

Thanks


"Michael Nemtsev" <ne*****@msn.com> wrote in message
news:9c**************************@msnews.microsoft .com...
Hello HarvSather,

You need compare classes' member not the classes instances.
H> C# VS2005
H> I have a class with 5 properties
H> I am adding instances this class to an ArrayList
H> I want to test to see if an instance is already in the ArrayList
H> before attempting to add the item
H> From my reading, it seems that I have to have an Equals method in my
H> class
H> I have created an Equals method, but it is never called
H> Does anyone have some insight into my problem?

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do
not cease to be insipid." (c) Friedrich Nietzsche

May 8 '06 #4
Well, I found the answer

I was declaring the Equals method with the wrong signature

Initially I had
public bool Equals(clsValueListItems vobjItem)

but it should be
public override bool Equals(object vobjItem)

The parameter must be a generic object rather than a specific type
Then you can cast it to a local variable and of the type you want
The whole Equals method looks like this in my class

public override bool Equals(object vobjItem)
{
if (vobjItem == null || GetType() != vobjItem.GetType())
return false;

clsValueListItems lobjItem = (clsValueListItems)vobjItem;
bool lblnIsContained = false;

if (lobjItem.ItemDataString == mstrItemData ||
(lobjItem.ItemDataInt == mintItemData && mintItemData != -10))
lblnIsContained = true;

return lblnIsContained;
}
The Contains method of an ArrayList uses the Equals method of the class to
determine if the item is in the list.
You have to override the base Equals method so that it works for the class
your are using

Harvey Sather
Saskatoon Canada



"HarvSather" <ha********@msn.com> wrote in message
news:uD**************@TK2MSFTNGP04.phx.gbl...
In the Equals method, I compare the properties of two instances.
I want to know if one instance is the same as another.
The thing that tells me that is if one of the properties in each instance
is equal.

The thing is, the equals method never gets called.

So, I am not sure if we are talking about the same thing.

Do you have some sample "working" code that you can post?

Thanks


"Michael Nemtsev" <ne*****@msn.com> wrote in message
news:9c**************************@msnews.microsoft .com...
Hello HarvSather,

You need compare classes' member not the classes instances.
H> C# VS2005
H> I have a class with 5 properties
H> I am adding instances this class to an ArrayList
H> I want to test to see if an instance is already in the ArrayList
H> before attempting to add the item
H> From my reading, it seems that I have to have an Equals method in my
H> class
H> I have created an Equals method, but it is never called
H> Does anyone have some insight into my problem?

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do
not cease to be insipid." (c) Friedrich Nietzsche


May 8 '06 #5
There might be a problem here with some of your code ...

if (vobjItem == null || GetType() != vobjItem.GetType())
return false;

What if I later what to subclass your type?

The following code should be a bit more friendly with polymorphism ..

clsValueListItems tmp = vobjItem as clsValueListItems;
if(tmp == null) {
return false;
}
//continue as normal.
Cheers,

Greg Young
MVP - C#

"HarvSather" <ha********@msn.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Well, I found the answer

I was declaring the Equals method with the wrong signature

Initially I had
public bool Equals(clsValueListItems vobjItem)

but it should be
public override bool Equals(object vobjItem)

The parameter must be a generic object rather than a specific type
Then you can cast it to a local variable and of the type you want
The whole Equals method looks like this in my class

public override bool Equals(object vobjItem)
{
if (vobjItem == null || GetType() != vobjItem.GetType())
return false;

clsValueListItems lobjItem = (clsValueListItems)vobjItem;
bool lblnIsContained = false;

if (lobjItem.ItemDataString == mstrItemData ||
(lobjItem.ItemDataInt == mintItemData && mintItemData != -10))
lblnIsContained = true;

return lblnIsContained;
}
The Contains method of an ArrayList uses the Equals method of the class to
determine if the item is in the list.
You have to override the base Equals method so that it works for the class
your are using

Harvey Sather
Saskatoon Canada



"HarvSather" <ha********@msn.com> wrote in message
news:uD**************@TK2MSFTNGP04.phx.gbl...
In the Equals method, I compare the properties of two instances.
I want to know if one instance is the same as another.
The thing that tells me that is if one of the properties in each instance
is equal.

The thing is, the equals method never gets called.

So, I am not sure if we are talking about the same thing.

Do you have some sample "working" code that you can post?

Thanks


"Michael Nemtsev" <ne*****@msn.com> wrote in message
news:9c**************************@msnews.microsoft .com...
Hello HarvSather,

You need compare classes' member not the classes instances.
H> C# VS2005
H> I have a class with 5 properties
H> I am adding instances this class to an ArrayList
H> I want to test to see if an instance is already in the ArrayList
H> before attempting to add the item
H> From my reading, it seems that I have to have an Equals method in my
H> class
H> I have created an Equals method, but it is never called
H> Does anyone have some insight into my problem?

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do
not cease to be insipid." (c) Friedrich Nietzsche



May 8 '06 #6

"HarvSather" <ha********@msn.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Well, I found the answer

I was declaring the Equals method with the wrong signature

Initially I had
public bool Equals(clsValueListItems vobjItem)

but it should be
public override bool Equals(object vobjItem)


Always use intellisense - it saves a lot of problems - You can start the
line with "override" rather than public and it will sort it all out nicely
for you when you've decided what you are overriding.

Of course if you had put override in anyway the compiler would have told you
that you weren't overriding anything.
May 8 '06 #7
Hi,

Why don't you use one of the generic implementations?

You can use ArrayList.Contains , if does use Equals . So yes, you need to
have an Equals method in your class.

Can you post your Equals ethod?

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"HarvSather" <ha********@msn.com> wrote in message
news:uH**************@TK2MSFTNGP02.phx.gbl...
C# VS2005

I have a class with 5 properties

I am adding instances this class to an ArrayList

I want to test to see if an instance is already in the ArrayList before
attempting to add the item

From my reading, it seems that I have to have an Equals method in my class

I have created an Equals method, but it is never called

Does anyone have some insight into my problem?

TIA

Harvey Sather
Saskatoon Canada

May 8 '06 #8
Take a look at the IEquatable<T> interface. Perhaps that will help.

May 8 '06 #9

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

Similar topics

1
by: thechaosengine | last post by:
Hi all, Can anyone tell me how ArrayList.Contains works. The docs say that it tells you if an *object* is contained in the list. Now, by object do the docs mean the exact instance that you...
10
by: C Downey | last post by:
Hello: I have an arraylist storing some very basic objects. The object is very basic, it has 2 properties : ID, and COUNT Before I add an object to the arraylist, I want to check if an...
4
by: tma | last post by:
I'm trying to save code to a session object like the following: dim oAppList as arraylist dim oApp as someclass Code to manipulate oApp... .... .... oApplist.Add(oApp)...
9
by: Leon | last post by:
I have a webform in which when the user press generate button the form generate six unique ramdon numbers, the user can also type these six numbers in manually in any order. however, the user can...
4
by: Neven Klofutar | last post by:
Hi, I'm trying to use Contains method, but I have some problems ... // create SqlDataReader SqlDataReader dr = .... // create collection of myObjects MyCollection myObjectsColl = new...
20
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the...
12
by: Robert Hooker | last post by:
We are moving to .NET2.0 - and have noticed that .Contains() seems to be around 20% slower under NET2, compared to NET1.1. This code shows the issue: static void Main(string args) {...
14
by: Kym | last post by:
Hey, I have an arraylist which I have added a number of objects to (all the same type of object), I need to be able to check if the array already contain the object before adding it, I have tried...
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
7
by: Kamran Shafi | last post by:
Hi, I am creating an arraylist (say masterArrayList) of hashtables, where each hashtable (say table) is of the format key=string, value = arraylist of strings (say existing_strings). In a...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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,...
0
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...
0
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...

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.