Calling a method at runtime

  • Thread starter Thread starter Les Hughes
  • Start date Start date
I want to call a method dynamically based on a string

For example

When s = "A" Then method A would get called. I don't wan't to use a
bunch of "If" statements.

Something like:

someMethod = AddressOf s 's = A

someMethod(SomeSortOfString)


Sub A(ByRef SomeString As String)
...
End Sub

Sub B(ByRef SomeString As String)
...
End Sub

Is this possible?
 
Rico

Of Course

Case select myMethode
Case "A"
A
Case "B"
B
End Select

Sub A(ByRef SomeString As String)
...
End Sub

Sub B(ByRef SomeString As String)
....
End Sub

I hope this helps?

Cor
 
Assuming that the signatures of the methods are the same, you can use a
delegate to do so...

Dim someMethodDelegate(SomeSortOfString As String) As [Delegate]

someMethodDelegate = AddressOf A
someMethodDelegate(SomeSortOfString)

....

Sub A(SomeString As String)
....
End Sub

Sub B(SomeString As String)
....
End Sub

HTH

David Williams [VB.NET MVP]
 
There may be a way of doing this, I'm sure Ive seen it answered before but
it really is i'll advised to take this approach

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
Hi David,

And than build a complete program around it to prevent runtime errors?
Sometimes technical methods are simple however if it is a solutions is often
the question.

Just my thought.

Cor
 
Hi David,

Yes, that is what I am looking for... one question. When you had:
someMethodDelegate = AddressOf A

Method 'A' is hard coded... I needed something like

someMethodDelegate = AddressOf s

s is a String that could have A or B or C (which are methods). In
other words I don't know what method to call until runtime and based
on the string, then the method would get called. All signatures will
be the same.

Thanks
Rico

Assuming that the signatures of the methods are the same, you can use a
delegate to do so...

Dim someMethodDelegate(SomeSortOfString As String) As [Delegate]

someMethodDelegate = AddressOf A
someMethodDelegate(SomeSortOfString)

...

Sub A(SomeString As String)
...
End Sub

Sub B(SomeString As String)
...
End Sub

HTH

David Williams [VB.NET MVP]


Rico Rivera said:
I want to call a method dynamically based on a string

For example

When s = "A" Then method A would get called. I don't wan't to use a
bunch of "If" statements.

Something like:

someMethod = AddressOf s 's = A

someMethod(SomeSortOfString)


Sub A(ByRef SomeString As String)
...
End Sub

Sub B(ByRef SomeString As String)
...
End Sub

Is this possible?
 
* (e-mail address removed) (Rico Rivera) scripsit:
I want to call a method dynamically based on a string

Have a look at 'CallByName', altneratively there is a way using
reflection.
 
Rico,

I use the following where MethodName is the name of the method to invoke.

<code (VB.NET) >


Dim aType As Type = SomeClass.GetType
Dim Args() as Object = {Arg1, Arg2,...}
Dim aFunctionReturn As Object
aFunctionReturn = aType.InvokeMember(MethodName _
, Reflection.BindingFlags.InvokeMethod
_
, Nothing _
, aFeatureClass.Instance _
, Args)
Return aFunctionReturn

</code>

In my applications everything is a "plugin", so the "host" application looks
at the database to determine the users available options. From this list I
load the appropriate classes at runtime and invoke a standard set of methods
on the class which are defined by an interface.

-Sam Matzen

-Sam Matzen
 
I don't know about that. I use delegates like this all the time and
catch any exceptions the same what as without delegates. I find that
the delegates help make the code cleaner.

David

Cor Ligthert said:
Hi David,

And than build a complete program around it to prevent runtime errors?
Sometimes technical methods are simple however if it is a solutions is
often
the question.

Just my thought.

Cor
Assuming that the signatures of the methods are the same, you can use
a
delegate to do so...

Dim someMethodDelegate(SomeSortOfString As String) As [Delegate]

someMethodDelegate = AddressOf A
someMethodDelegate(SomeSortOfString)

...

Sub A(SomeString As String)
...
End Sub

Sub B(SomeString As String)
...
End Sub
 
That is fine. Just be sure to catch any exceptions that happen when "s"
is not a valid function name or the function that is referenced by "s"
has the wrong signature. I do this all the time.

HTH

David

Rico Rivera said:
Hi David,

Yes, that is what I am looking for... one question. When you had:
someMethodDelegate = AddressOf A

Method 'A' is hard coded... I needed something like

someMethodDelegate = AddressOf s

s is a String that could have A or B or C (which are methods). In
other words I don't know what method to call until runtime and based
on the string, then the method would get called. All signatures will
be the same.

Thanks
Rico

Assuming that the signatures of the methods are the same, you can use a

delegate to do so...

Dim someMethodDelegate(SomeSortOfString As String) As [Delegate]

someMethodDelegate = AddressOf A
someMethodDelegate(SomeSortOfString)

...

Sub A(SomeString As String)
...
End Sub

Sub B(SomeString As String)
...
End Sub

HTH

David Williams [VB.NET MVP]


Rico Rivera said:
I want to call a method dynamically based on a string

For example

When s = "A" Then method A would get called. I don't wan't to use a
bunch of "If" statements.

Something like:

someMethod = AddressOf s 's = A

someMethod(SomeSortOfString)


Sub A(ByRef SomeString As String)
...
End Sub

Sub B(ByRef SomeString As String)
...
End Sub

Is this possible?
 
Back
Top