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 8 9515
"HarvSather " <ha********@msn .com> wrote in message
news:uH******** ******@TK2MSFTN GP02.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
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
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.co m> wrote in message
news:9c******** *************** ***@msnews.micr osoft.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
Well, I found the answer
I was declaring the Equals method with the wrong signature
Initially I had
public bool Equals(clsValue ListItems 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.GetTyp e())
return false;
clsValueListIte ms lobjItem = (clsValueListIt ems)vobjItem;
bool lblnIsContained = false;
if (lobjItem.ItemD ataString == mstrItemData ||
(lobjItem.ItemD ataInt == 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******** ******@TK2MSFTN GP04.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.co m> wrote in message news:9c******** *************** ***@msnews.micr osoft.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
There might be a problem here with some of your code ...
if (vobjItem == null || GetType() != vobjItem.GetTyp e())
return false;
What if I later what to subclass your type?
The following code should be a bit more friendly with polymorphism ..
clsValueListIte ms tmp = vobjItem as clsValueListIte ms;
if(tmp == null) {
return false;
}
//continue as normal.
Cheers,
Greg Young
MVP - C#
"HarvSather " <ha********@msn .com> wrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. .. Well, I found the answer
I was declaring the Equals method with the wrong signature
Initially I had public bool Equals(clsValue ListItems 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.GetTyp e()) return false;
clsValueListIte ms lobjItem = (clsValueListIt ems)vobjItem; bool lblnIsContained = false;
if (lobjItem.ItemD ataString == mstrItemData || (lobjItem.ItemD ataInt == 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******** ******@TK2MSFTN GP04.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.co m> wrote in message news:9c******** *************** ***@msnews.micr osoft.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
"HarvSather " <ha********@msn .com> wrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. .. Well, I found the answer
I was declaring the Equals method with the wrong signature
Initially I had public bool Equals(clsValue ListItems 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.
Hi,
Why don't you use one of the generic implementations ?
You can use ArrayList.Conta ins , 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******** ******@TK2MSFTN GP02.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
Take a look at the IEquatable<T> interface. Perhaps that will help. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 pass in, or some other
way of working?
I have a situation whereby i have a custom collection extended from an ArrayList,
and I would like to be able to find out if a collection contains an instance
|
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 object with
that same ID already exists in the arraylist.
If it does, I would like to increase the count of the matching object inside
|
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)
session.add("AppList", oApplist)
|
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 also press a post button
that post these six numbers into the database. My problem is that these six
numbers need to be posted to the database from less to greatest, and all of
my code is within the business tier class not the code behind class.
...
|
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 MyCollection();
| |
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 arraylist then I have to handle this
with a sort method that uses comparer. I can reference the properties of the
Arraylist directly such as
dim mylist as new FrameList
mylist.Add(new FrameStructure)
mylist(0).first = "blabla..."
mylist(0).second...
|
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)
{
System.Collections.ArrayList list = new System.Collections.ArrayList();
//Setup code
//
|
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 something like...
if Not myArray.contains(newObject) then
myArray.Add(newObject)
end if
This will work with a string object but not a user defined object.
|
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:
code:------------------------------------------------------------------------------
class Program
{
static public ArrayList MyArrayList = new ArrayList();
|
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 foreach loop I
retreive the corresponding hashtable from the masterArrayList and add
new strings to existing_strings after comparing the hashtable keys.
The problem is that when I add a new value to an existing_strings
arraylist it also add the new...
|
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,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
|
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,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |