VB.Net List.Find。将值传递给谓词

VB.Net List.Find。将值传递给谓词,vb.net,Vb.net,使用列表时遇到一些问题。使用自定义谓词查找 Private Shared Function FindByOldKeyAndName(ByVal k As KeyObj) As Boolean If k.OldKey = currentKey.OldKey And k.KeyName = currentKey.KeyName Then Return True Else Return False End

使用列表时遇到一些问题。使用自定义谓词查找

Private Shared Function FindByOldKeyAndName(ByVal k As KeyObj) As Boolean
        If k.OldKey = currentKey.OldKey And k.KeyName = currentKey.KeyName Then
            Return True
        Else
            Return False
        End If


    End Function
我有一个函数可以做到这一点

private function test ()
    Dim test As Integer = keys.Find(AddressOf FindByOldKeyAndName).NewKey
下面是谓词的函数

Private Shared Function FindByOldKeyAndName(ByVal k As KeyObj) As Boolean
        If k.OldKey = currentKey.OldKey And k.KeyName = currentKey.KeyName Then
            Return True
        Else
            Return False
        End If


    End Function
这样做意味着我必须在类中有一个共享的“currentKey”对象,并且我知道必须有一种方法来传递我感兴趣的currentKey的值(即keyname和oldkey)

理想情况下,我想用这样的方式来称呼它
keys.Find(FindByOldKeyAndName(Name,OldVal))的地址

但是,当我这样做时,会出现编译器错误


如何调用此方法并传入值?

您可以使用VS2008及更高版本中提供的lambda表达式清楚地解决此问题。一个愚蠢的例子:

Sub Main()
    Dim lst As New List(Of Integer)
    lst.Add(1)
    lst.Add(2)
    Dim toFind = 2
    Dim found = lst.Find(Function(value As Integer) value = toFind)
    Console.WriteLine(found)
    Console.ReadLine()
End Sub

对于早期版本,您必须将“currentKey”设置为类的私有字段。请检查我的代码以获得更干净的解决方案。

我不需要在VB.Net的较新版本中尝试这一点,这可能有更好的方法,但在较旧的版本中,我知道的唯一方法是在类中有一个共享成员在调用之前设置值。

网络上有各种各样的示例,人们创建了一些小的实用程序类来将其包装起来,使其更加美观。

我有一个对象,它管理一系列独特的属性类型。 例如:

下面是一些检查属性是否已经存在的代码

Public Sub AddProperty(ByVal prop As PropertyClass)
    If Properties.Count < 50 Then
        'Lets verify this property does not exist
        Dim existingProperty As PropertyClass = _
            Properties.Find(Function(value As PropertyClass)
                Return value.PropertyType = prop.PropertyType
            End Function)

        'if it does not exist, add it otherwise throw exception
        If existingProperty Is Nothing Then
            Properties.Add(prop)
        Else
            Throw New DuplicatePropertyException("Duplicate Property: " + _
                         prop.PropertyType.ToString())
        End If

    End If
End Sub
Public子AddProperty(ByVal prop作为PropertyClass)
如果属性数小于50,则
'让我们验证此属性不存在
Dim existingProperty作为PropertyClass=_
查找(函数(值为PropertyClass)
返回值.PropertyType=prop.PropertyType
终端功能)
'如果它不存在,请添加它,否则将引发异常
如果existingProperty为空,则
属性。添加(道具)
其他的
抛出新的DuplicatePropertyException(“重复属性:”+_
prop.PropertyType.ToString())
如果结束
如果结束
端接头

我发现一个博客有一个更好的“真实世界”上下文示例,有很好的变量名

在列表中查找对象的关键代码如下:

      ' Instantiate a List(Of Invoice).
        Dim invoiceList As New List(Of Invoice)

        ' Add some invoices to List(Of Invoice).
        invoiceList.Add(New Invoice(1, DateTime.Now, 22))
        invoiceList.Add(New Invoice(2, DateTime.Now.AddDays(10), 24))
        invoiceList.Add(New Invoice(3, DateTime.Now.AddDays(30), 22))
        invoiceList.Add(New Invoice(4, DateTime.Now.AddDays(60), 36))

        ' Use a Predicate(Of T) to find an invoice by its invoice number.
        Dim invoiceNumber As Integer = 1
        Dim foundInvoice = invoiceList.Find(Function(invoice) invoice.InvoiceNumber = invoiceNumber)
有关更多示例,包括日期搜索,请参阅Mike McIntyre的博客