|
-
Jun 5th, 2025, 04:20 PM
#1
Custom Comparator Cursing
I have a Dictionary(Of ISomething, SomeOtherThing)
The ISomething interface includes a property Endpoint, which returns a GUID.
I wanted to use ContainsKey to see whether some ISomething was already in the dictionary, but was unable to get it to work. At first, I went with the documentation which suggested that the object underlying ISomething had to implement IEqualityComparer(of ISomething).
That was easy enough to implement, and it didn't work. An ISomething could be added when it was already a key in the dictionary (it was a new object, so it wouldn't duplicate the key, but the Endpoint properties were the same).
A bit of time on the interwebs showed a couple examples of creating a CustomComparator class that implemented the IEqualityComparer, and which was passed to the Dictionary constructor. This I did like so:
Code:
Public Class CustomComparer
Implements IEqualityComparer(Of ISomething)
Public Function Equals2(x As ISomething, y As ISomething) As Boolean Implements IEqualityComparer(Of ISomething).Equals
Return x.Endpoint= y.Endpoint
End Function
Public Function GetHashCode1(<DisallowNull> obj As ISomething) As Integer Implements IEqualityComparer(Of ISomething).GetHashCode
Return CType(obj, Object).GetHashCode
End Function
End Class
This, too, did not work. Despite there being an ISomething as a key with GUID X, the Dictionary would welcome in a new ISomething with GUID X.
The dictionary was declared as such, per what I found on the web:
Code:
Private mPreviousReddSet As New Dictionary(Of ISomething, IRedd)(New CustomComparer)
Frankly, I doubt this much matters. Using the custom comparator is probably no faster, and not much more compact, than just iterating through all the keys looking at that endpoint property. Still, I'd like to understand what is going on here. Did I write something incorrectly?
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|