tags:

views:

33

answers:

1

I've check everywhere and can't find a solution. I have the following

Dim users as New List(of TUser)

Private Sub AddSelectedUsers()

For Each user as TUser in gridSelectedItems()

If Not users.Contains(user) Then

users.Add(user)

End If

Next

End Sub 

The "Contains" is not working. I keep getting duplicates on the users list.

+1  A: 

The List.Contains() method uses the default equality operator. Since TUser appears to be a class, List.Contains() will only match if you are referencing the exact same instance of TUser in both comparisons, apparently isn't the case. See a more detailed explanation here.

The solution is to implement an equality override for the TUser class, as per the example here.

Alex
I suspected that might've been the problem because gridSelectedItems() is a list passed "ByRef" to this form. I'll give this a shot.
Saif Khan
I ended up overriding the Equals in my TUser class and that works.
Saif Khan