473,378 Members | 1,346 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,378 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 24259
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.