473,398 Members | 2,125 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,398 software developers and data experts.

Call a function whose name is stored in a string?

Hi all,

Is it possible to execute a function by getting the name of the function to
execute from a string? For example:

string FunctionName = "MyFunction";

which would execute MyFunction().

Any help appreciated,
Cheers, Hugh
Nov 15 '05 #1
1 21679
"Hugh" <hy**@softhome.net> wrote in message
news:eM**************@TK2MSFTNGP09.phx.gbl...
Hi all,

Is it possible to execute a function by getting the name of the function to execute from a string? For example:

string FunctionName = "MyFunction";

which would execute MyFunction().


Yes. Here's an example that calls System.Random.NextDouble, where the method
name "NextDouble" comes from a string.

class Class1
{
static Random rand = new Random();

static void Main(string[] args)
{
string methodName = "NextDouble";
System.Reflection.MethodInfo info =
rand.GetType().GetMethod(methodName);
double r = (double) info.Invoke(rand, null);
}
}

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mariusz | last post by:
I want to write function to call another function which name is parameter to first function. Other parameters should be passed to called function. If I call it function('f1',10) it should call...
7
by: Andrzej | last post by:
Is it possible to call a function which name is given by a string? Let assume that I created a program which call some functions for example void f1(void), void f2(void), void f3(void). ...
1
by: The_Kingpin | last post by:
Hi all, I need to make a function that convert a string into a certain format. Here what are the restriction: -The first letter of the first and last name must be uppercase. -If a first name...
2
by: Abdul Qadir Khan | last post by:
Hi All, Is it possible to call a function whose name is store in variable using VB.Net. Assume this dim a as string a = "GetEmployee()" Now I have function name in a variable, how can I...
5
by: H | last post by:
dim subName = "GetTable" & strCtr & "()" I want to now call the GetTable1() sub or GetTable2() sub, depending on the value of strCtr. How can I call the function whose name is in the variable...
6
by: CptDondo | last post by:
I think the answer to this is "no" but I figured I'd try.... I have a function name as a string: funcName = "xyz"; and I have a function: void xyz(blah) {}
5
by: Patrick | last post by:
Hi! I want kwnow if it's possible to call a function giving an another function the name (in string format). Something like this: var sRoutine = window.Form1.Text1.value; //for exampre the...
11
by: dolphin | last post by:
Hi All! I have a question that how to call a function just using a string. For example There is a .cpp file named a.cpp.There are some functions::fun1() fun2() fun3(). I have another fucntion...
7
by: Andrew Poulos | last post by:
Say I have this foo = function() { // blah }; bar = fuction(fnName) { /* If fnName equalled "foo" * How do I test that foo, as a function, * exists and then, if it exists, call it?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.