VB.NET内联委托谓词不起作用

时间:2012-01-27 10:41:07

标签: .net vb.net delegates .net-2.0 predicate

这是我的问题。如果我写这个 -

Private ListValue As Object = Nothing

Private Sub FindIndex(ByVal e As ListBoxFindItemArgs)
    e.IsFound = Object.Equals(ListValue, e.ItemValue)
End Sub

Private Sub SearchValues
    ListValue  = 5
    Index = Me.lst_department.FindItem(0, True, AddressOf FindIndex)
End Sub

但是我只是出于机智,为什么写这些代码的代码不能正常工作 -

Private Sub SearchValues
    ListValue  = 5
    Index = Me.lst_department.FindItem(0, True, Function(e As ListBoxFindItemArgs) e.IsFound = Object.Equals(ListValue, e.ItemValue))
End Sub

1 个答案:

答案 0 :(得分:4)

因为你的“谓词”不是 函数 1 ,所以它是Sub。如果您使用的是最新版本的VB,则可以编写以下内容:否则,你运气不好:

Index = Me.lst_department.FindItem(0, True, Sub(e As ListBoxFindItemArgs) e.IsFound = Object.Equals(ListValue, e.ItemValue))

1 此外,它不是谓词。谓词是一种特定类型的函数,对某些类型Function(x As T) As Boolean具有签名T

相关问题