473,796 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with ListViewItemCol lection 'Contains' method

Hi!

I have trouble with 'Contains' method in ListViewItemCol lection 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.Collecti ons;
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.ListVi ewItemCollectio n col = new ListView.ListVi ewItemCollectio n(new ListView());
col.Add(new MyClass("1"));
col.Add(new MyClass("2"));

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

class MyClass : System.Windows. Forms.ListViewI tem
{
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 ListViewItemsCo llection don't.
Any ideas?

Cheers,
Piotrek

Aug 19 '05 #1
2 2032
ListViewItemCol lection.Contain s does the equivalent of calling
Object.Referenc eEquals when comparing an item to the items in the list, so it
does not call Object.Equals. This means that ListViewItemCol lection.Contain s
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 ListViewItemCol lection 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.Collecti ons;
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.ListVi ewItemCollectio n col = new ListView.ListVi ewItemCollectio n(new ListView());
col.Add(new MyClass("1"));
col.Add(new MyClass("2"));

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

class MyClass : System.Windows. Forms.ListViewI tem
{
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 ListViewItemsCo llection don't.
Any ideas?

Cheers,
Piotrek

Aug 19 '05 #2
Hi!
ListViewItemCol lection.Contain s does the equivalent of calling
Object.Referenc eEquals when comparing an item to the items in the list, so it
does not call Object.Equals. This means that ListViewItemCol lection.Contain s
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
1150
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 ettListViewItemCollection : ListView.ListViewItemCollection { } the compile error this this:
0
3942
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
6
3815
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 is more that 1249 bytes, then the progress bar of the browser will move too slow and then displaying that the page not found!!!! If the message is less than or equal to 1249 then no problem.
0
1540
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 great, except... Private Sub tvBookmarks_BeforeSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles tvBookmarks.BeforeSelect
2
381
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 doesn't override 'Equals' method, but I _DO_ override it in my class. Have a look at thread:...
2
4455
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 */ #include <time.h>
9
8164
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() the resulting file 'Text.txt' has the same length than the string 'temp'.
8
11994
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 as that returned from Active Directory. For example using 'cn=' in the userdn string instead of 'CN=' does not work. As far as I am aware Active Directory is not case sensitive and it is therefore difficult to predict the case of a string.
2
24299
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 method will return a generic list containing only the distinct names. This fails in my code Example: public class Car
0
10457
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, 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...
1
10176
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,...
0
10013
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9054
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, 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...
1
7550
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6792
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();...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2927
bsmnconsultancy
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...

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.