Last post Sep 08, 2008 09:17 AM by silas2
Sep 08, 2008 07:51 AM|silas2|LINK
Public Sub TestCollections4()
Dim mine As New SortedList(Of Fish, String)(New CustomComparer)
Dim Peter As New Fish : Peter.finger = "ab-Peter"
Dim John As New Fish : John.finger = "beastly- John"
Dim Simon As New Fish : Simon.finger = "s --pat"
Dim Patricia As New Fish : Patricia.finger = "p --pat"
mine(Simon) = "s"
mine(Peter) = "j"
mine(John) = "k"
mine(Patricia) = "p"
If mine.ContainsKey(Simon) Then 'not returning true
Dim ex As Integer
For Each Trumpton As KeyValuePair(Of Fish, String) In mine
Implements IComparer(Of Fish)
Implements IEqualityComparer(Of Fish)
Public Function Compare(ByVal x As Fish, ByVal y As Fish) As Integer Implements System.Collections.Generic.IComparer(Of Fish).Compare
Return Len(y.finger) - Len(x.finger)
Public Function Equals1(ByVal x As Fish, ByVal y As Fish) As Boolean Implements System.Collections.Generic.IEqualityComparer(Of Fish).Equals
Return x.finger = y.finger ' never gets called???
Public Function GetHashCode1(ByVal obj As Fish) As Integer Implements System.Collections.Generic.IEqualityComparer(Of Fish).GetHashCode
Public finger As String
Sep 08, 2008 08:01 AM|DMW|LINK
It's almost certainly because your Equals1 method doesn't implement IComparer.Equals. Once you get it to do that, I'm sure that it will be called.
Sep 08, 2008 08:21 AM|silas2|LINK
Thank you for your comment, my understanding was that you need to override:
IComparer.Equals which I don't think exists on that interface.
If indeed you did mean IEqualityComparer.Equals, I think if you slide the code window to the right you will see that it is being implemented, unless I misunderstand you.the
Sep 08, 2008 08:45 AM|silas2|LINK
Although I think this confusion might be the root of the problem. The MSDN documentation is not very clear on it, but there are two comparisons - one for the sorting (IComparer) and one for equality (IEqualityComparer). The constructor for the IDictionary
family seems to take either one or the other, but of course you can envisage needing both at once, so I combined them both in the same class, which may not be feasible. I don't know what else you are supposed to do.
Sep 08, 2008 09:17 AM|silas2|LINK
I'm sorry, you're right it is getting called.