P
Piotr Szukalski
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/micr...contains+equals&rnum=2&hl=pl#75351591db8d07c5
.... 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
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/micr...contains+equals&rnum=2&hl=pl#75351591db8d07c5
.... 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