Custom Generic.IEqualityComparer (Of T) - Compiler Errors

I am trying to implement a simple IEqulityComparer for use with LINQ collections. I wrote the following code, which boils down to its simplest form for discussion ...

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 is that both functions throw a warning to the compiler “getHashCode function” (or “Equals”) obscures the overridden method in the base class “Object”. To override a base class method, this method must be declared "Overrides".

However, if I declare them as overrides, I get the error "GetHashCode function" cannot be declared Overrides, because it does not override the function in the base class. "

"", "getHashCode", , .

, - - - , ?

+3
4

, , , . Overloads.

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
+3

, , "", , . , .

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
+1

, VB.NET, #. #, , , , .

VB.NET , , , . ( ), "".

+1

I have the same problem. I am converting my C # code to VB.Net; Even adding tools did not help; Using shadow or overload removes all warnings and errors. I wonder what the difference in behavior is in both cases. If I specify Overrides, I get an error. Without specifying any of (overrides, overloads, shadows), a warning is issued.

' <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
+1
source

Source: https://habr.com/ru/post/1710667/


All Articles