【发布时间】:2009-10-07 07:27:56
【问题描述】:
我发现自己经常从 lambda 调用函数,因为提供的委托不匹配或没有足够的参数。令人恼火的是,我不能对子程序执行 lambda。每次我想这样做时,我都必须将我的子例程包装在一个不返回任何内容的函数中。不漂亮,但它有效。
是否有另一种方法可以使这更流畅/更漂亮?
我已经读到整个 lambda 不足可能会在 VS2010/VB10 中得到修复,所以我的问题更多是出于好奇。
一个简单的例子:
Public Class ProcessingClass
Public Delegate Sub ProcessData(ByVal index As Integer)
Public Function ProcessList(ByVal processData As ProcessData)
' for each in some list processData(index) or whatever'
End Function
End Class
Public Class Main
Private Sub ProcessingSub(ByVal index As Integer, _
ByRef result As Integer)
' (...) My custom processing '
End Sub
Private Function ProcessingFunction(ByVal index As Integer, _
ByRef result As Integer) As Object
ProcessingSub(index, result)
Return Nothing
End Function
Public Sub Main()
Dim processingClass As New ProcessingClass
Dim result As Integer
' The following throws a compiler error as '
' ProcessingSub does not produce a value'
processingClass.ProcessList( _
Function(index As Integer) ProcessingSub(index, result))
' The following is the workaround that'
' I find myself using too frequently.'
processingClass.ProcessList( _
Function(index As Integer) ProcessingFunction(index, result))
End Sub
End Class
【问题讨论】: