向 VB.NET 中的通用列表的 FindAll 添加参数

发布于 2024-08-11 08:25:47 字数 268 浏览 7 评论 0原文

很好的问题和有用的答案:

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

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

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

Excellent question and useful looking answers at:

Adding a parameter to a FindAll for a Generic List in C#

But can anyone help turn Jon Skeet's assistance into valid .NET 2.0 VB?

I have run his answers through a couple of the usual CSharp converters but the results don't compile.

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

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

发布评论

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

评论(4

波浪屿的海角声 2024-08-18 08:25:47

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

编辑添加了运算符枚举以赋予其更多的灵活性。您应该能够从那里扩展。

    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

Create a wrapper class that inherits from whatever Generic List you want. Then, overload the FindAll method.

Edit Added operator Enum to give it a little more flexibility. You should be able to extend from there.

    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
帅的被狗咬 2024-08-18 08:25:47

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

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

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

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

A more generic solution is make a generic helper class, and initialize them with the reference value.

Because, if you need a integer list, and in another hand a double list, you must implement two classes, but, with this approach you use only one.

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

With this code, you create the helper and you could encapsulate the comparison logic.

从﹋此江山别 2024-08-18 08:25:47

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

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

Based on the explanations above and from further digging around ... seems the straight answer is there's no direct VB translation. VBers have to go the long way round, at least in VS2005.

In the end we used the Paul Stovell example which was the clearest solution I could find. This is working fine for us.

無處可尋 2024-08-18 08:25:47

我认为你可以做到这一点

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

I think you can do this

objectList.FindAll(AddressOf MyFunc)

Function MyFunc (ByVal item As testObject)
   Return item._groupLevel = desiredGroupLevel
End Function

This is off the top of my head, so I'm not sure if it's correct. But the idea is that you can use AddressOf to call another routine to perform actions on each item in the list.

EDIT: Code example:

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