在VB中实现泛型IComparer [英] Implementing generic IComparer in VB
问题描述
我试图创建一个类来实现我自己的类Stellungen的通用IComparer(它翻译成位置,就像棋子或跳棋板上的那样)。
这是我得到的:
Private Class comparer(Of Stellung)
实现System.Collections.Generic.IComparer (Of Stellung)
公共函数比较(x作为Stellung,y作为Stellung)作为整数实现System.Collections.Generic.IComparer(Of Stellung).Compare
End Function
End Class
问题是:函数内部我无法访问我班的任何领域。如果我从x开始。 Intellisense只会给我.Equals,.GetHashCode - 你在类型上获得的方法,但不在实例上。
Visual Studio 10也很重要,在函数的定义中,x作为Stellung和y作为Stellung是用浅蓝色书写的,这意味着它是一种类型而不是实际的对象。
那么...我该怎么做?我如何访问我想在课堂内比较的东西?
Thanks!
这些字段可能是私人的,这就是您不能访问它们的原因。
让你的类实现 IComparable< T>
界面。您可以在比较器类中使用它。
这是一个通用比较器类的示例,它比较实现 IComparable< T>
的对象。
公共类GenericComparer(Of T As IComparable(Of T))
继承Comparer(Of T)
Public Overrides Function [Compare](ByVal x As T,ByVal y As T)As Integer
If(Not x Is Nothing)Then
If(Not y Is Nothing)然后
返回x.CompareTo(y)
End If
返回1
End If
If(Not y Is Nothing)然后
返回-1
End如果
返回0
End Function
公共覆盖函数等于(ByVal obj As Object)As Boolean
Dim comparer As GenericComparer(Of T)= TryCast(obj, GenericComparer(Of T))
返回(不是比较器是Nothing)
结束函数
公共覆盖函数GetHashCode()A s Integer
返回MyBase.GetType.Name.GetHashCode
End Function
结束类
公共类Stellung
实现IComparable(Of Stellung)
公共函数CompareTo(ByVal value As Stellung)As Integer
'在这里您应该可以访问所有字段。
End Function
End class
I am trying to create a class implementing the generic IComparer of my own class "Stellungen" (which translates to positions, like on a chess or checkers board).
This is what I got:
Private Class comparer(Of Stellung)
Implements System.Collections.Generic.IComparer(Of Stellung)
Public Function Compare(x As Stellung, y As Stellung) As Integer Implements System.Collections.Generic.IComparer(Of Stellung).Compare
End Function
End Class
Problem is: inside the function I have no access to any fields of my class. If I start off with x. Intellisense will only give me .Equals, .GetHashCode - the methods you get on a type but not on an instance. Visual Studio 10 also highights this, in the definition of the function the bits "x as Stellung" and "y as Stellung" are written in light blue, meaning it is a type and not an actual object.
So... what do I do?? How do I access the things I want to compare inside my class?? Thanks!
The fields are probably private and that is why you cant access them.
Make you classes implement the IComparable<T>
interface. You can than use that in you comparer class.
Here is an example of a generic comparer class that compares objects that implements IComparable<T>
.
Public Class GenericComparer(Of T As IComparable(Of T))
Inherits Comparer(Of T)
Public Overrides Function [Compare](ByVal x As T, ByVal y As T) As Integer
If (Not x Is Nothing) Then
If (Not y Is Nothing) Then
Return x.CompareTo(y)
End If
Return 1
End If
If (Not y Is Nothing) Then
Return -1
End If
Return 0
End Function
Public Overrides Function Equals(ByVal obj As Object) As Boolean
Dim comparer As GenericComparer(Of T) = TryCast(obj,GenericComparer(Of T))
Return (Not comparer Is Nothing)
End Function
Public Overrides Function GetHashCode() As Integer
Return MyBase.GetType.Name.GetHashCode
End Function
End Class
public class Stellung
Implements IComparable(Of Stellung)
Public Function CompareTo(ByVal value As Stellung) As Integer
'Here you should be able to access all fields.
End Function
End class
这篇关于在VB中实现泛型IComparer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!