gpt4 book ai didi

vb.net - 如何在VB NET中将方法名称作为过程的参数传递

转载 作者:行者123 更新时间:2023-12-04 03:09:16 24 4
gpt4 key购买 nike

我想创建一个过程,它的参数也是一个过程。是否可以?
我创建了一些过程用作以下参数:

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.net - 如何在VB NET中将方法名称作为过程的参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8697504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com