[英]How to Pass MethodName as Parameter of a Procedure in 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找到。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.