views:

619

answers:

3

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?

+1  A: 

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
FourOaks
+1  A: 

You're getting the compiler error because you are in VB.NET, not C#. In C#, having a method with the same signature as an interface method you need to implement makes the compiler wire it up for you automatically.

VB.NET requires that you explicitly implement say what method is being implemented. You can use the same name (it's encouraged), you just have to have that 'implements' clause.

Jonathan
A: 

I am getting same problem. I am converting my C# code to VB.Net; Even Adding the Implements didn't help; Using a shadow or overload removes all warning and errors. I wonder what is difference in behavior in both cases. If I specify Overrides, I get an error. Not specifying any of (overrides, overloads, shadows) gives a warning.

' <summary>
' Pair Comparator for maintaining uniquness in results.
' </summary>
Public Class PairComparer
    Implements IEqualityComparer(Of Pair)
    Public Shadows Function Equals(ByVal x As Pair, ByVal y As Pair) As Boolean Implements System.Collections.Generic.IEqualityComparer(Of Pair).Equals

        If x.first = y.first AndAlso x.second = y.second Then
            Equals = True
        ElseIf x.first = y.second AndAlso x.second = y.first Then
            Equals = True
        Else
            Equals = False
        End If
    End Function

    Public Overloads Function GetHashCode(ByVal obj As Pair) As Integer Implements System.Collections.Generic.IEqualityComparer(Of Pair).GetHashCode
        GetHashCode = obj.first + obj.second
    End Function
End Class
Asad Iqbal