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

发布于 2024-08-06 17:21:37 字数 1447 浏览 4 评论 0原文

我发现自己经常从 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻风 2024-08-13 17:21:37

如果您发现您经常使用相同类型的数据,那么您可以将委托包装在一个类中。

创建一个转换为委托的基类:

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

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
小红帽 2024-08-13 17:21:37

它已在 VB10 中修复,VS10 Beta 已可用(如果有)一个供您使用的选项。在 VB10 中,您有没有返回值的 lambda 表达式和内联子函数/函数。

现在,也许您可​​以忘记 lambda 并使用委托来代替?像这样的东西:

processingClass.ProcessList(AddressOf ProcessingSub)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文