2

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

在 C# 中为通用列表的 FindAll 添加参数

但是任何人都可以帮助将 Jon Skeet 的帮助变成有效的 .NET 2.0 VB 吗?

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

4

4 回答 4

2

创建一个从您想要的任何通用列表继承的包装类。然后,重载 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
于 2009-11-17T12:51:39.677 回答
1

一个更通用的解决方案是创建一个通用的辅助类,并使用参考值初始化它们。

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

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

使用此代码,您可以创建帮助程序并封装比较逻辑。

于 2009-11-17T17:10:55.333 回答
0

我认为你可以做到这一点

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
于 2009-11-17T09:51:17.423 回答
0

基于上面的解释和进一步挖掘......似乎直接的答案是没有直接的VB翻译。VBers 必须走很长的路,至少在 VS2005 中。

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

于 2009-11-20T14:59:23.027 回答