自定义 Generic.IEqualityComparer(Of T) - 编译器错误

发布于 2024-07-23 23:50:31 字数 868 浏览 9 评论 0原文

我正在尝试实现一个简单的 IEqulityComparer 来与 LINQ 集合一起使用。 我编写了以下代码,出于讨论目的,将其简化为最简单的形式...

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

我遇到的问题是,这两个函数都会引发编译器警告“函数‘getHashCode’(或‘Equals’)隐藏了基础中的可重写方法类“Object”。要重写基类方法,必须将该方法声明为“Overrides”。”

但是,如果我将它们声明为 Overrides,则会收到错误“函数 'GetHashCode' 无法声明为 Overrides,因为它不会覆盖基类中的函数。”!!

我还在“Implements”行上收到编译器错误,大意是我必须实现“getHashCode”,但我认为这是第一个问题的结果。

我所有的研究表明我应该没问题 - 有人有任何线索吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苦行僧 2024-07-30 23:50:31

这是问题的较晚答案,但根据文档,您可以使用以下内容。 请注意包含 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

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
满身野味 2024-07-30 23:50:31

好吧,似乎可以通过重命名函数并将它们声明为“实现”来进行排序,尽管我在网上看到了数十个示例,但情况并非如此。
不过,我现在收到一个运行时异常,我将单独发布该异常。

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

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
以歌曲疗慰 2024-07-30 23:50:31

我遇到了同样的问题。 我正在将 C# 代码转换为 VB.Net; 即使添加工具也无济于事;
使用阴影或重载可以消除所有警告和错误。 我想知道这两种情况下的行为有何不同。
如果我指定 Overrides,则会收到错误。
不指定任何(覆盖、重载、阴影)都会发出警告。

' <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

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
歌枕肩 2024-07-30 23:50:31

您收到编译器错误是因为您使用的是 VB.NET,而不是 C#。 在 C# 中,如果方法具有与需要实现的接口方法相同的签名,则编译器会自动为您连接它。

VB.NET 要求您明确实现正在实现的方法。 您可以使用相同的名称(这是鼓励的),您只需要有“implements”子句即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文