vb.net – 如何将MethodName作为VBNET中的过程参数传递

前端之家收集整理的这篇文章主要介绍了vb.net – 如何将MethodName作为VBNET中的过程参数传递前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想创建一个过程,它的参数也是一个过程.可能吗?
我创建了一些程序用作以下参数:
Private Sub Jump(xStr as string)
    MsgBox xStr & " is jumping."
End Sub

Private Sub Run(xStr as string)
    MsgBox xStr & " is jumping."
End Sub

这个程序应该调用以上程序:

Private Sub ExecuteProcedure(?,StringParameter) '- i do not know what to put in there
     ? ' - name of the procedure with parameter
End Sub

用法

ExecuteProcedure(Jump,"StringName")
 ExecuteProcedure(Run,"StringName")
我相信以下代码是您需要的一个例子.
Public Delegate Sub TestDelegate(ByVal result As TestResult)

Private Sub RunTest(ByVal testFunction As TestDelegate)

     Dim result As New TestResult
     result.startTime = DateTime.Now
     testFunction(result)
     result.endTime = DateTime.Now

End Sub

Private Sub MenuItemStartTests_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MenuItemStartTests.Click

     Debug.WriteLine("Starting Tests...")
     Debug.WriteLine("")
     '==================================
     ' Add Calls to Test Modules Here

     RunTest(AddressOf Test1)
     RunTest(AddressOf Test2)

     '==================================

     Debug.WriteLine("")
     Debug.WriteLine("Tests Completed")

End Sub

整个文章可以在http://dotnetref.blogspot.com/2007/07/passing-function-by-reference-in-vbnet.html找到

希望这可以帮助.

猜你在找的VB相关文章