Problem with ListViewItemCollection 'Contains' method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Piotr Szukalski

    #1

    Problem with ListViewItemCollection '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:


    .... 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

  • Jakob Christensen

    #2
    RE: Problem with ListViewItemCol lection 'Contains' method

    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.
    --




    "Piotr Szukalski" wrote:
    [color=blue]
    > 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
    >
    >[/color]

    Comment

    • Piotr Szukalski

      #3
      RE: Problem with ListViewItemCol lection 'Contains' method

      Hi!
      [color=blue]
      > 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.[/color]

      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

      Comment

      Working...