如何将MethodName作为VBNET中过程的参数传递 [英] How to Pass MethodName as Parameter of a Procedure in VBNET

查看:99
本文介绍了如何将MethodName作为VBNET中过程的参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Procedure,它的参数也是一个过程。可能吗?

我创建了一些程序作为参数使用:

I want to create a Procedure that its parameter is also a procedure. Is it possible?
I created some procedures to be used as parameters below:

Private Sub Jump(xStr as string)
    Msgbox xStr & " is jumping."
End Sub

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

此过程应该调用上述过程:

this procedure should call the procedure above:

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")

推荐答案

我相信以下代码是您需要的示例。

I believe the following code is an example for what you need.

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

希望这有助于。

这篇关于如何将MethodName作为VBNET中过程的参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆