473,385 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to call a function where function name is a variable

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
5 1895
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
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
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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: schaf | last post by:
Hi ! I'm writing a new xsl:function, which uses two other functions. But by the call of the first function, it would be abort just after the call. Not even the xsl:param would be set. I don't...
5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
5
by: Rob | last post by:
Help me, I'm just beginning with programming in Access 2000. I've tried the http://www.mvps.org/access/api/api0001.htm but it won't work in Access. What am i doing wrong. I don't have...
12
by: Baldy | last post by:
really tricky one here. I want to be able to call a function but the function name is in a variable. The code is a security module that is querying a table that stores names of functions, forms,...
5
by: Kurt Van Campenhout | last post by:
Hi, I am trying to get/set Terminal server information in the active directory on a windows 2000 domain. Since the ADSI calls for TS don't work until W2K3, I need to do it myself. I'm fairly...
5
by: Matt Clepper | last post by:
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...
3
by: mikea_59 | last post by:
How can I generate element names by calling a function? Something like this: <xsl:element name="XYZ:{call_some_local_function_here}"> Do I need to put the function reply in a variable and...
7
by: Steve_Black | last post by:
Hello, I'm toying with the idea of loading a MenuStrip (VB.Net 2005) dynamically based on who is logged into my system. Every user has different security settings and I want to customize the...
0
by: mix01 | last post by:
Hi, I am trying to get some VBA code working, but am preplex as to why it does not work. I would really appreciate any level of help. Many thanks, Mix01 Version of the program
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.