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

Problem using List.Contains()

..NET 2.0

I'm trying to use List.Contains to determine if an object already exist in
the list. This Contains method always return false in my code

In my code a method gets a list of names, the method will return a generic
list containing only the distinct names. This fails in my code

Example:
public class Car
[Serializable]
private string _name;
public Car(string name) { _name = name; }
}

List<Carcars = List<Car>();
Car car = new Car("Volvo");
list.Add(car);
car = new Car("Volvo");
if (!list.Contains(car))
List.Add(car) <<<<<<<<<<<< here is the problem in my code, Here
Contains didn't find the car("Volvo") already in the list, so this
if-statement fails and another car(volvo) object is added to the list

Maybe I instead should use Exists?

Any suggestions are welcome
Aug 1 '07 #1
2 24267
Jeff wrote:
.NET 2.0

I'm trying to use List.Contains to determine if an object already exist in
the list. This Contains method always return false in my code

In my code a method gets a list of names, the method will return a generic
list containing only the distinct names. This fails in my code

Example:
public class Car
[Serializable]
private string _name;
public Car(string name) { _name = name; }
}

List<Carcars = List<Car>();
Car car = new Car("Volvo");
list.Add(car);
car = new Car("Volvo");
if (!list.Contains(car))
List.Add(car) <<<<<<<<<<<< here is the problem in my code, Here
Contains didn't find the car("Volvo") already in the list, so this
if-statement fails and another car(volvo) object is added to the list

Maybe I instead should use Exists?

Any suggestions are welcome
The Contains method checks if an item exists in the list. As this is a
list of references, it checks if the reference exists in the list.

The two Car objects that you create are separate objects, so they have
different references. The Contains method only looks for the reference,
so it doesn't find the existing object although it contains the same data.

Use a Dictionary<string, Car>, where the key is the name of the car.

--
Göran Andersson
_____
http://www.guffa.com
Aug 1 '07 #2
On Aug 1, 1:19 pm, Göran Andersson <gu...@guffa.comwrote:
The Contains method checks if an item exists in the list. As this is
a list of references, it checks if the reference exists in the list.
No - it checks for items which are deemed to be equal. As Equals
hasn't been overridden, the default implementation is used, which is
just for reference equality. Your statement doesn't hold in general
though - a good example is a list of strings, where two separate
string objects with the same data are equal, and so List.Contains will
find one string given the other.

Jon

Aug 1 '07 #3

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

Similar topics

0
by: d | last post by:
Hey all, I've been having a weird problem using com objects in IIS 4. I've built a middleware system that can be called with XML requests over HTTP. The request is sent to an ASP page which...
4
by: Derek Timothy | last post by:
Hi folks I have strange problem using cmd.exe from asp code I am trying to save the results of an FTP command into a text file. When I run this command from the command line of the web server it...
1
by: Brian Pelton | last post by:
I feel like I'm about to get in over my head, so before I go off the deep end I'm hoping someone can point me in the right direction. I started down the road of using a List<> of an Interface...
2
by: sianan | last post by:
I am having a problem doing the following in generics. I have two list of a custom item type. I need to iterate through the first list and match each item against another list to see if there is...
0
by: nineeshk | last post by:
I have a search query from my MSSQL database. My problem is when i search the database using the text string which contains any umlaut character. FOr eg. When i search for Cömlek. It gives the...
1
by: jitupatil2007 | last post by:
hi friends i am reading a csv file using string array and after that to keep a validation check i have added the array to list and using list i am accessing the records correctly but i have a...
2
by: ihimitsu | last post by:
Hi friends guide me to sorting java list contains multiple list objects
11
by: mathieu | last post by:
Hi there, I am trying to write something very simple to test if a list contains another one: a = b = but 'a in b' returns False. How do I check that a is indeed contained
3
by: presencia | last post by:
Hi all, I have a problem using gdb. I am using Eclipse-CDT as a frontend, but since this problem is obviously related to the gcc compiler or the gdb itself, I have reproduced it without Eclipse. ...
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
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
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
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
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...
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.