自定义 Generic.IEqualityComparer(Of T) - 编译器错误
我正在尝试实现一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是问题的较晚答案,但根据文档,您可以使用以下内容。 请注意包含 Overloads 关键字。
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.
好吧,似乎可以通过重命名函数并将它们声明为“实现”来进行排序,尽管我在网上看到了数十个示例,但情况并非如此。
不过,我现在收到一个运行时异常,我将单独发布该异常。
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.
我遇到了同样的问题。 我正在将 C# 代码转换为 VB.Net; 即使添加工具也无济于事;
使用阴影或重载可以消除所有警告和错误。 我想知道这两种情况下的行为有何不同。
如果我指定 Overrides,则会收到错误。
不指定任何(覆盖、重载、阴影)都会发出警告。
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.
您收到编译器错误是因为您使用的是 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.