开发者

Calling Subroutines from lambda in vb.net

I find myself calling functions from lambdas frequently as the provided delegate does not match or does not have sufficient parameters. It is irritating that I cannot do lambda on subroutines. Each time I want to do this I have to wrap my subroutine in a function which returns nothing. Not pretty, but it works.

Is there another way of doing this that makes this smoother/prettier?

I have read that this whole lambda inadequacy will probably be fixed in VS2010/VB10 so my question is more out of curiosity.

A simple Example:

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


If you find that you are doing it too often and generally with the same type of data, you can wrap the delegate in a class.

Create a base class that converts to the delegate:

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

Inherit from the 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

Use it:

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


It IS fixed in VB10, the VS10 Beta is available, if it's an option for you to use it. In VB10 you have lambdas without a return value, and inline subs/functions.

For now, maybe you could just forget lambdas and work with delegates instead? Something like:

processingClass.ProcessList(AddressOf ProcessingSub)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜