Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Pass MethodName as Parameter of a Procedure in VBNET

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

usage:

 ExecuteProcedure(Jump, "StringName")
 ExecuteProcedure(Run, "StringName")
like image 407
John Woo Avatar asked Jan 02 '12 04:01

John Woo


People also ask

How do you pass parameter for a procedure?

To pass one or more arguments to a procedure In the calling statement, follow the procedure name with parentheses. Inside the parentheses, put an argument list. Include an argument for each required parameter the procedure defines, and separate the arguments with commas.

What is parameter passing in VB net?

In Visual Basic, you can pass an argument to a procedure by value or by reference. This is known as the passing mechanism, and it determines whether the procedure can modify the programming element underlying the argument in the calling code.

Can procedures take parameters?

We can create a procedure with parameters to handle both the repetition and the variance. To specify parameters in JavaScript, we write the name of the parameter (or parameters) inside the parentheses that come after the function name. We then reference that parameter name inside the function.

How do you define a parameter in VBA?

In the procedure declaration, add the parameter name to the procedure's parameter list, separating it from other parameters by commas. Decide the data type of the parameter. Follow the parameter name with an As clause to specify the data type. Decide the passing mechanism you want for the parameter.


1 Answers

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

The entire article can be found at http://dotnetref.blogspot.com/2007/07/passing-function-by-reference-in-vbnet.html

Hope this helps.

like image 170
Gerald P. Wright Avatar answered Oct 14 '22 05:10

Gerald P. Wright