By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,647 Members | 1,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,647 IT Pros & Developers. It's quick & easy.

How to call a function where function name is a variable

P: n/a
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
Nov 10 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
"Matt Clepper" <Ma*********@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
| 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
|
|
Nov 10 '05 #2

P: n/a
Wouldn't delegates also work in this case?

"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
message news:%2****************@TK2MSFTNGP12.phx.gbl...
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
"Matt Clepper" <Ma*********@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
| 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
|
|

Nov 10 '05 #3

P: n/a
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
"Flip" <ph******@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
| Wouldn't delegates also work in this case?
|
|
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
| message news:%2****************@TK2MSFTNGP12.phx.gbl...
| > 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
| >
| >
| > "Matt Clepper" <Ma*********@discussions.microsoft.com> wrote in message
| > news:C3**********************************@microsof t.com...
| > | 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
| > |
| > |
| >
| >
|
|
Nov 10 '05 #4

P: n/a

You can do this using reflection.

MethodInfo mInfo = this.GetType().GetMethod("myMethod");
Matt Clepper wrote:
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

Nov 10 '05 #5

P: n/a

And after that:

mInfo.Invoke(this, userParameters);
John A. Bailo wrote:

You can do this using reflection.

MethodInfo mInfo = this.GetType().GetMethod("myMethod");
Matt Clepper wrote:
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

Nov 10 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.