473,404 Members | 2,174 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,404 software developers and data experts.

Problem with ListViewItemCollection 'Contains' method

Hi!

I have trouble with 'Contains' method in ListViewItemCollection class - it
seems like it nevers calls 'Equals' method of class inherited from
ListViewItem... I've found that ListViewItem doesn't override 'Equals'
method, but I _DO_ override it in my class. Have a look at thread:
http://groups.google.com/group/micro...351591db8d07c5

.... and at this piece of code:

using System;
using System.Collections;
using System.Windows.Forms;

namespace CollectionsTest
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
ArrayList ar = new ArrayList();
ar.Add(new MyClass("1"));
ar.Add(new MyClass("2"));

ListView.ListViewItemCollection col = new ListView.ListViewItemCollection(new ListView());
col.Add(new MyClass("1"));
col.Add(new MyClass("2"));

System.Console.WriteLine("Contains: {0}", ar.Contains(new MyClass("1")));
System.Console.WriteLine("Contains: {0}", col.Contains(new MyClass("1")));
System.Console.ReadLine();
}
}

class MyClass : System.Windows.Forms.ListViewItem
{
private string str;

public MyClass(string s)
{
str = s;
}

public override bool Equals(object obj)
{
return str.Equals(obj.ToString());
}

public override int GetHashCode()
{
return str.GetHashCode ();
}

public override string ToString()
{
return str;
}
}
}

ArrayList do calls my 'Equals' method, but ListViewItemsCollection don't.
Any ideas?

Cheers,
Piotrek

Aug 19 '05 #1
2 1993
ListViewItemCollection.Contains does the equivalent of calling
Object.ReferenceEquals when comparing an item to the items in the list, so it
does not call Object.Equals. This means that ListViewItemCollection.Contains
only compares references. You will have to implement your own comparison if
you consider two items with the same data to be equivalent.

HTH, Jakob.
--
http://www.dotninjas.dk
http://www.powerbytes.dk
"Piotr Szukalski" wrote:
Hi!

I have trouble with 'Contains' method in ListViewItemCollection class - it
seems like it nevers calls 'Equals' method of class inherited from
ListViewItem... I've found that ListViewItem doesn't override 'Equals'
method, but I _DO_ override it in my class. Have a look at thread:
http://groups.google.com/group/micro...351591db8d07c5

.... and at this piece of code:

using System;
using System.Collections;
using System.Windows.Forms;

namespace CollectionsTest
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
ArrayList ar = new ArrayList();
ar.Add(new MyClass("1"));
ar.Add(new MyClass("2"));

ListView.ListViewItemCollection col = new ListView.ListViewItemCollection(new ListView());
col.Add(new MyClass("1"));
col.Add(new MyClass("2"));

System.Console.WriteLine("Contains: {0}", ar.Contains(new MyClass("1")));
System.Console.WriteLine("Contains: {0}", col.Contains(new MyClass("1")));
System.Console.ReadLine();
}
}

class MyClass : System.Windows.Forms.ListViewItem
{
private string str;

public MyClass(string s)
{
str = s;
}

public override bool Equals(object obj)
{
return str.Equals(obj.ToString());
}

public override int GetHashCode()
{
return str.GetHashCode ();
}

public override string ToString()
{
return str;
}
}
}

ArrayList do calls my 'Equals' method, but ListViewItemsCollection don't.
Any ideas?

Cheers,
Piotrek

Aug 19 '05 #2
Hi!
ListViewItemCollection.Contains does the equivalent of calling
Object.ReferenceEquals when comparing an item to the items in the list, so it
does not call Object.Equals. This means that ListViewItemCollection.Contains
only compares references. You will have to implement your own comparison if
you consider two items with the same data to be equivalent.


Thanks for your answer. I still consider it strange that one
implementation of IList works one way and another seems to follow other
rules... Well, I'm not M$ to make the law... However I wish to find such
information in docs, because it took a while to find bug in my code.

Many thanks,
Piotrek

Aug 19 '05 #3

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

Similar topics

0
by: Chris LaJoie | last post by:
I'm unable to inherit from ListView.ListViewItemCollection because of a compile error. There's nothing in the class, so I can't imagine what it doesn't like about it. public class...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
0
by: Andre Viens | last post by:
Hello, I have a TreeView I'm using to navigate through various options shown in a ListView. I want to use the SelectedNode.Tag property to hold the items in my ListView. Everything is working...
2
by: Piotr Szukalski | last post by:
Hi! I have trouble with 'Contains' method in ListViewItemCollection class - it seems like it nevers calls 'Equals' method of class inherited from ListViewItem... I've found that ListViewItem...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
9
by: philip | last post by:
If I execute that : Dim Temp as string = "This is a text" Dim sw As StreamWriter Dim fullFileName as string = "c:\text.txt" sw = New StreamWriter(fullFilename) sw.Write(temp) sw.Close() ...
8
by: Chris Noble | last post by:
I need to check whether a particular user is already a member of an Active Directory Security Group. The following code extract works but only if the user distinguished name is exactly the same...
2
by: Jeff | last post by:
..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...
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: 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: 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
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
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.