473,804 Members | 2,124 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 2034
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
3946
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
3818
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
1541
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
4457
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
8166
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
11997
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
24301
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
9715
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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,...
0
10603
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
10356
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
10099
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...
1
7643
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
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
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 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.