Contains method doesn't work on BindingList<T>

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a BindingList<T> collection and I am adding concrete object T in to
the binding list. Before adding them, I want to verify if object with same
data exist in the collection. When I use Binding list’s Contain method to
check if object exist in the collection, it always returns false. Is there a
way to achieve this? I have tried implementing IComparable interface on my
concrete class, but getting same result.

Code sample:

BindingList<User> uBL=new BindingList<User>();

User u=new User();
u.FirstName=â€TTTâ€;
u.LastName=â€CCCâ€;
if (! uBL.Contains(u)) //this always returns false
{
uBL.Add(u)
}

Public class User{

Public string FirstName=string.empty;
Public string LastName=string.empty;
Public User(){}

}


Thanks
Hiten
 
Hiten said:
Hi,

I have a BindingList<T> collection and I am adding concrete object T in to
the binding list. Before adding them, I want to verify if object with same
data exist in the collection. When I use Binding list's Contain method to
check if object exist in the collection, it always returns false. Is there a
way to achieve this? I have tried implementing IComparable interface on my
concrete class, but getting same result.

Try implementing IEquatable inT.

The docs are bugged (I have told them, who knows how long it will take
them to hear me) - many (I think all) of the generic Contains methods
use an EqualityComparer, not Comparer.Default, for checking
containment. Therefore it is IEquatable, not IComparable, that the
contained types must implement for Contains to work (although
implementing IComparable might be required or useful for other
reasons).
 
Back
Top