Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Generic.IEqualityComparer(Of T) - Compiler Errors

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?

like image 586
FourOaks Avatar asked Jun 17 '09 11:06

FourOaks


2 Answers

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
like image 77
Jason Avatar answered Oct 14 '22 19:10

Jason


Ok, it seems to get sorted by renaming the functions and declaring them as "Implements", although I have seen dozens of examples on the Web where this has not been the case. However I now get a runtime exception which I will post separately.

Public Class insCompare
    Implements System.Collections.Generic.IEqualityComparer(Of Object)

    Public Function Equals1(ByVal x As Object, ByVal y As Object) As Boolean Implements System.Collections.Generic.IEqualityComparer(Of Object).Equals
        Return IIf(x.SiteID = y.SiteID, True, False)

    End Function

    Public Function GetHashCode1(ByVal x As Object) As Integer Implements System.Collections.Generic.IEqualityComparer(Of Object).GetHashCode
        Return x.SiteID.ToString.ToLower.GetHashCode()

    End Function

End Class
like image 41
FourOaks Avatar answered Oct 14 '22 19:10

FourOaks