Invoke a Function - with Parameters

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I use this code to invoke a function from a string, which I build in my application:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Public <span style="color:Blue; Class Form1
<span style="color:Blue; Dim f <span style="color:Blue; As <span style="color:Blue; New Foo
<span style="color:Blue; Dim methodName <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Dim result <span style="color:Blue; As <span style="color:Blue; Object
<span style="color:Blue; Private <span style="color:Blue; Sub Button1_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles Button1.Click
<span style="color:Blue; Try
methodName = <span style="color:#A31515; "HelloWorld1"
result = <span style="color:Blue; GetType(Foo).GetMethod(methodName).Invoke(f, <span style="color:Blue; Nothing)
MessageBox.Show(result.ToString)
<span style="color:Blue; Catch ex <span style="color:Blue; As NullReferenceException
MessageBox.Show(ex.ToString, <span style="color:#A31515; "Null Reference")
<span style="color:Blue; Catch ex <span style="color:Blue; As InvalidCastException
MessageBox.Show(ex.ToString, <span style="color:#A31515; "Invalid Cast")
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
MessageBox.Show(ex.ToString, <span style="color:#A31515; "Any Exception")
<span style="color:Blue; End <span style="color:Blue; Try

<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; End <span style="color:Blue; Class


<span style="color:Blue; Public <span style="color:Blue; Class Foo
<span style="color:Blue; Public <span style="color:Blue; Function HelloWorld1() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Return <span style="color:#A31515; "Hello World - One"
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; Public <span style="color:Blue; Function HelloWorld2() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Return <span style="color:#A31515; "Hello World - Two"
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; End <span style="color:Blue; Class

[/code]
<br/>
This worls fine.
Now to increase functionality: How can I pass a Parameter to the function (same Parameter to any Function)

Thx for help <hr class="sig Its no problem, its just the syntax

View the full article
 
Back
Top