Generics 为VB.NET中的泛型列表向FindAll添加参数

Generics 为VB.NET中的泛型列表向FindAll添加参数,generics,predicate,c#-to-vb.net,Generics,Predicate,C# To Vb.net,优秀的问题和有用的答案: 但是,有人能帮助将Jon Skeet的帮助转换为有效的.NET 2.0 VB吗 我已经通过几个常见的CSharp转换器运行了他的答案,但结果无法编译。我想你可以这样做 objectList.FindAll(AddressOf MyFunc) Function MyFunc (ByVal item As testObject) Return item._groupLevel = desiredGroupLevel End Function 这是我脑子里想不出来

优秀的问题和有用的答案:

但是,有人能帮助将Jon Skeet的帮助转换为有效的.NET 2.0 VB吗


我已经通过几个常见的CSharp转换器运行了他的答案,但结果无法编译。

我想你可以这样做

objectList.FindAll(AddressOf MyFunc)

Function MyFunc (ByVal item As testObject)
   Return item._groupLevel = desiredGroupLevel
End Function
这是我脑子里想不出来的,所以我不确定它是否正确。但是,您可以使用AddressOf调用另一个例程来对列表中的每个项目执行操作

编辑:代码示例:

Module Module1

    Sub Main()
        Dim source As New List(Of Integer)
        source.Add(1)
        source.Add(2)
        source.Add(3)
        source.Add(4)

        Dim newList As List(Of Integer) = source.FindAll(AddressOf MyFunc)

    End Sub

    Function MyFunc(ByVal item As Integer) As Boolean
        Return item > 3
    End Function

End Module

创建一个从您想要的任何泛型列表继承的包装器类。然后,重载FindAll方法

编辑添加了运算符Enum,使其更具灵活性。你应该可以从那里扩展

    Module Module1

    Sub Main()
        Dim source As New IntList
        source.Add(1)
        source.Add(2)
        source.Add(3)
        source.Add(4)

        Dim newList As List(Of Integer) = source.FindAll(IntList.Operators.GreaterThan, 2)

        For Each i As Integer In newList
            Console.WriteLine(i.ToString)
        Next

        Console.WriteLine("Press any key..............")
        Console.ReadLine()
    End Sub

End Module

Public Class IntList
    Inherits Generic.List(Of Integer)

    Enum Operators
        Equal
        NotEqual
        GreaterThan
        GreaterThanEqualTo
        LessThan
        LessThanEqualTo
    End Enum

    Private _Val As Integer = Nothing
    Private _Op As Operators = Nothing

    Public Overloads Function FindAll(ByVal [Operator] As Operators, ByVal Val As Integer) As List(Of Integer)
        _Op = [Operator]
        _Val = Val
        Return MyBase.FindAll(AddressOf MyFunc)
    End Function

    Function MyFunc(ByVal item As Integer) As Boolean
        Select Case _Op
            Case Operators.Equal
                Return item = _Val
            Case Operators.NotEqual
                Return item <> _Val
            Case Operators.GreaterThan
                Return item > _Val
            Case Operators.GreaterThanEqualTo
                Return item >= _Val
            Case Operators.LessThan
                Return item < _Val
            Case Operators.LessThanEqualTo
                Return item <= _Val
        End Select
    End Function
End Class
模块1
副标题()
将源变暗为新的IntList
来源.添加(1)
来源.添加(2)
来源.添加(3)
来源.添加(4)
Dim newList As List(Of Integer)=source.FindAll(IntList.Operators.GreaterThan,2)
对于newList中作为整数的每个i
Console.WriteLine(i.ToString)
下一个
控制台写入线(“按任意键”)
Console.ReadLine()
端接头
端模块
公共类IntList
继承泛型.List(整数的)
枚举运算符
相等的
诺特奎尔
大于
伟大的塔尼科尔托
莱斯坦
莱斯坦尼科尔托
结束枚举
Private _Val作为整数=无
Private _opas Operators=无
公共重载函数FindAll(ByVal[Operator]作为运算符,ByVal Val作为整数)作为(整数的)列表
_Op=[运算符]
_瓦尔=瓦尔
返回MyBase.FindAll(MyFunc的地址)
端函数
函数MyFunc(ByVal项为整数)为布尔值
选择案例_Op
Case操作符。相等
返回项=_Val
Case.NotEqual
退货项目
案例操作员。大于
返回项目>\u Val
Case Operators.GreaterThanEqualTo
返回项目>=\u Val
莱斯特尚
退货项目<\u Val
Case Operators.LessThanEqualTo

returnitem一个更通用的解决方案是创建一个通用的helper类,并用引用值初始化它们

因为,如果您需要一个整数列表,另一方面需要一个双列表,那么您必须实现两个类,但是,使用这种方法,您只能使用一个类

Module Question1747687
    Class OperatorHelper(Of refType)
        Public ReferenceValue As refType

        Sub New(ByVal value As refType)
            ReferenceValue = value
        End Sub

        Public Function Equal(ByVal comp As refType) As Boolean
            Return ReferenceValue.Equals(comp)
        End Function

        Public Function NotEqual(ByVal comp As refType) As Boolean
            Return Not ReferenceValue.Equals(comp)
        End Function

        Public Function GreaterThan(ByVal comp As refType) As Boolean
            Return Compare(comp, ReferenceValue) > 0
        End Function

        Public Function GreaterThanEqualTo(ByVal comp As refType) As Boolean
            Return Compare(comp, ReferenceValue) >= 0
        End Function

        Public Function LessThan(ByVal comp As refType) As Boolean
            Return Compare(comp, ReferenceValue) < 0
        End Function

        Public Function LessThanEqualTo(ByVal comp As refType) As Boolean
            Return Compare(comp, ReferenceValue) <= 0
        End Function

        Private Function Compare(ByVal l As refType, ByVal r As refType) As Integer
            Return CType(l, IComparable).CompareTo(CType(r, IComparable))
        End Function
    End Class

    Sub Main()
        Dim source As New List(Of Integer)
        Dim helper As OperatorHelper(Of Integer)

        source.Add(1)
        source.Add(2)
        source.Add(3)
        source.Add(4)

        helper = New OperatorHelper(Of Integer)(2)
        Dim newlist As List(Of Integer) = source.FindAll(AddressOf helper.LessThanEqualTo)

        For Each i As Integer In newlist
            Console.WriteLine(i.ToString)
        Next

        Console.ReadLine()
    End Sub
End Module
模块问题1747687
类OperatorHelper(属于refType)
作为refType的公共引用值
子新建(ByVal值作为refType)
参考值=参考值
端接头
公共函数Equal(ByVal comp作为refType)作为布尔值
返回ReferenceValue.Equals(comp)
端函数
公共函数NotEqual(ByVal comp作为refType)作为布尔值
返回Not ReferenceValue.Equals(comp)
端函数
作为布尔值的公共函数GreaterThan(ByVal comp作为refType)
返回比较(comp,ReferenceValue)>0
端函数
公共函数GreaterThanEqualTo(ByVal comp作为refType)作为布尔值
返回比较(comp,ReferenceValue)>=0
端函数
公共函数LessThan(ByVal comp作为refType)作为布尔值
返回比较(comp,ReferenceValue)<0
端函数
公共函数LessThanEqualTo(ByVal comp作为refType)作为布尔值

基于以上解释和进一步挖掘,返回比较(comp,ReferenceValue)。。。似乎直接的答案是没有直接的VB翻译。VBER必须走很长的路,至少在VS2005年是这样


最后我们使用了我能找到的最清晰的解决方案。这对我们来说很好。

是的,这很好,但它没有参数化,这才是真正的重点。“退货项目>3”行。。。如何参数化3。啊,我误解了这个要求。在这种情况下,您必须创建一个包装器函数,类似于brad的建议。