Custom Generic.IEqualityComparer(Of T) - Compiler Errors

compiler-warnings, iequalitycomparer, linq, vb.net

Solution

This is a late answer to the question but as per the documentation you can do use the following. Notice the inclusion of the Overloads keyword.

Public Class MyModelComparer
    Implements Generic.IEqualityComparer(Of MyModel)

    Public Overloads Function Equals(x As MyModel, y As MyModel) As Boolean Implements System.Collections.Generic.IEqualityComparer(Of MyModel).Equals
       ' do compare
    End Function

    Public Overloads Function GetHashCode(obj As MyModel) As Integer Implements System.Collections.Generic.IEqualityComparer(Of MyModel).GetHashCode
       ' do hashcode
    End Function

End Class

Problem

I am trying to implement a simple IEqulityComparer to use with LINQ collections. I have written the following code which is reduced to its simplest form for discussion purposes... ``` Public Structure bob Dim SiteID As Integer Dim fred As String End Structure Public Class insCompare Implements System.Collections.Generic.IEqualityComparer(Of bob) Public Function Equals(ByVal x As bob, ByVal y As bob) As Boolean Return IIf(x.SiteID = y.SiteID, True, False) End Function Public Function GetHashCode(ByVal x As bob) As Integer Return x.SiteID.GetHashCode() End Function End Class ``` The problem that I have is that both functions throw the compiler warning "function 'getHashCode' (or 'Equals') shadows an overridable method in the base class 'Object'. To override the base class method, this method must be declared 'Overrides'." However, if I declare them as Overrides, I get the error "function 'GetHashCode' cannot be declared Overrides because it does not override a function in the base class."!! I am also getting a compiler error on the "Implements" line to the effect that I must implement "getHashCode" but I presume that is a result of the first problem. All my research indicates that I should be ok - anyone got any clues please?

Original source