How to call a function where function name is a variable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Any way to do this? I need to call functions based on a variable. Do I
actually have to make a case statement and call each funciton explicitly, or
is there any way to call a function where the funciton name is a variable.

Example:

dim variable as string
variable = "thisfunction()"

call variable <---this will not work
 
Matt,
The "easiest" way is to use CallByName, something like:

Dim someobject As Object
Dim variable As String
variable = "thisfunction"
Dim result As Object
Dim param1 As Object
Dim param2 As Object

result = CallByName(someobject, variable, CallType.Method, param1,
param2)

Where "someobject" is an instance of an object that you want to call the
method against, can be nothing if you are calling a shared method.

"variable" is the name of the method
"result" is the value the method returns
"param1" is the first parameter
"param2" is the second parameter

Of course if you are calling a Sub or a Property Set, then result is not
used. Likewise param1 & param2 are only included for methods with
parameters, you can include a variable number of parameters as parameters is
a ParamArray argument.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Any way to do this? I need to call functions based on a variable. Do I
| actually have to make a case statement and call each funciton explicitly,
or
| is there any way to call a function where the funciton name is a variable.
|
| Example:
|
| dim variable as string
| variable = "thisfunction()"
|
| call variable <---this will not work
|
|
 
Flip,
Yes delegates would work, however are they "easier" then CallByName,
especially when you have the name of the routine in a String.

FWIW: I suspect CallByName is a thin wrapper over delegates.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Wouldn't delegates also work in this case?
|
|
|
| message | > Matt,
| > The "easiest" way is to use CallByName, something like:
| >
| > Dim someobject As Object
| > Dim variable As String
| > variable = "thisfunction"
| > Dim result As Object
| > Dim param1 As Object
| > Dim param2 As Object
| >
| > result = CallByName(someobject, variable, CallType.Method,
param1,
| > param2)
| >
| > Where "someobject" is an instance of an object that you want to call the
| > method against, can be nothing if you are calling a shared method.
| >
| > "variable" is the name of the method
| > "result" is the value the method returns
| > "param1" is the first parameter
| > "param2" is the second parameter
| >
| > Of course if you are calling a Sub or a Property Set, then result is not
| > used. Likewise param1 & param2 are only included for methods with
| > parameters, you can include a variable number of parameters as
parameters
| > is
| > a ParamArray argument.
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > | > | Any way to do this? I need to call functions based on a variable. Do
I
| > | actually have to make a case statement and call each funciton
| > explicitly,
| > or
| > | is there any way to call a function where the funciton name is a
| > variable.
| > |
| > | Example:
| > |
| > | dim variable as string
| > | variable = "thisfunction()"
| > |
| > | call variable <---this will not work
| > |
| > |
| >
| >
|
|
 
You can do this using reflection.

MethodInfo mInfo = this.GetType().GetMethod("myMethod");
 
Back
Top