在vb.net中从lambda调用子例程

在vb.net中从lambda调用子例程,vb.net,delegates,lambda,Vb.net,Delegates,Lambda,我发现自己经常从lambdas调用函数,因为提供的委托不匹配或没有足够的参数。我不能在子程序上运行lambda,这令人恼火。每次我想这样做,我必须把我的子程序包装在一个不返回任何内容的函数中。虽然不漂亮,但很管用 有没有其他方法可以让它更平滑/更漂亮 我已经读到,整个lambda的不足可能会在VS2010/VB10中得到修复,所以我的问题更多的是出于好奇 一个简单的例子: Public Class ProcessingClass Public Delegate Sub ProcessDa

我发现自己经常从lambdas调用函数,因为提供的委托不匹配或没有足够的参数。我不能在子程序上运行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

它是在VB10中修复的,VS10测试版是,如果您可以选择使用它的话。在VB10中,有没有返回值的lambda和内联sub/函数

现在,也许你可以忘记lambdas,转而与代表一起工作?比如:

processingClass.ProcessList(AddressOf ProcessingSub)

它是在VB10中修复的,VS10测试版是,如果您可以选择使用它的话。在VB10中,有没有返回值的lambda和内联sub/函数

现在,也许你可以忘记lambdas,转而与代表一起工作?比如:

processingClass.ProcessList(AddressOf ProcessingSub)

如果您发现这样做太频繁,并且通常使用相同类型的数据,那么可以将委托包装到类中

创建转换为委托的基类:

Public MustInherit Class ProcessDataBase
    Public Shared Widening Operator CType(operand As ProcessDataBase) as ProcessingClass.ProcessData
        Return AddressOf operand.Process
    End Sub

    Protected MustOverride Sub Process(index As Integer)  
End Class
从类继承:

Public Class ProcessResult
    Inherits ProcessDataBase

    Public Result As Integer

    Protected Overrides Sub Process(index as Integer)
        ' Your processing, result is modified.
    End SUb
End Class
使用它:

Public Class Main()
    Public Sub Main()
        Dim processingClass As New ProcessingClass
        Dim processor As New ProcessResult

        processingClass.ProcessList(processor)
        Dim result as integer=processor.Result
    End Sub
End Class

如果您发现这样做太频繁,并且通常使用相同类型的数据,那么可以将委托包装到类中

创建转换为委托的基类:

Public MustInherit Class ProcessDataBase
    Public Shared Widening Operator CType(operand As ProcessDataBase) as ProcessingClass.ProcessData
        Return AddressOf operand.Process
    End Sub

    Protected MustOverride Sub Process(index As Integer)  
End Class
从类继承:

Public Class ProcessResult
    Inherits ProcessDataBase

    Public Result As Integer

    Protected Overrides Sub Process(index as Integer)
        ' Your processing, result is modified.
    End SUb
End Class
使用它:

Public Class Main()
    Public Sub Main()
        Dim processingClass As New ProcessingClass
        Dim processor As New ProcessResult

        processingClass.ProcessList(processor)
        Dim result as integer=processor.Result
    End Sub
End Class

正如问题中所述,问题在于委托规范与实现不匹配。调用
processingClass.ProcessList(ProcessingSub的地址)
会产生编译时错误,因为
ProcessingSub(ByVal索引为整数,ByRef结果为整数)
与委托
ProcessData(ByVal索引为整数)
不能将ByRef结果添加到ProcessData参数中吗,然后将结果发送到ProcessList函数中?重点是ProcessingClass不必知道如何实现ProcessData。否则,就失去了委托的全部意义。我想我把我的例子简化了。现实的ProcessData委托应该采用某种数据结构作为参数。在不同的情况下,我有不同的ProcessData实现,具有不同的参数,而processingClass却愉快地不知道内部发生了什么……正如问题中所述,问题在于委托规范与实现不匹配。调用
processingClass.ProcessList(ProcessingSub的地址)
会产生编译时错误,因为
ProcessingSub(ByVal索引为整数,ByRef结果为整数)
与委托
ProcessData(ByVal索引为整数)
不能将ByRef结果添加到ProcessData参数中吗,然后将结果发送到ProcessList函数中?重点是ProcessingClass不必知道如何实现ProcessData。否则,就失去了委托的全部意义。我想我把我的例子简化了。现实的ProcessData委托应该采用某种数据结构作为参数。在不同的情况下,我有不同的ProcessData实现,具有不同的参数,而processingClass却愉快地不知道内部发生了什么……看到您的解决方案,我意识到我实际上在努力维护状态,这当然应该通过类来完成。在你的解决方案中,委托的参数不再重要,这使得它变得更加漂亮。看到你的解决方案,我意识到我实际上是在努力维护状态,这当然应该通过类来完成。对于您的解决方案,委托的参数不再重要,这使得它更漂亮。