Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a list of function names like do_work(),do_work1(),...

After prompting to user to take inputs my calculations generates a string that string is a function name which i have already implemented.

I am getting function name as a string, now I don't know how to call that function

Can anybody help me...
Posted

It requires a "trick" specific to scripting languages. In case of Python, this is the function eval():
https://docs.python.org/2/library/functions.html#eval[^].

See also:
http://en.wikipedia.org/wiki/Eval[^],
http://en.wikipedia.org/wiki/Eval#Theory[^].

Use it with care. Look at this article: http://lybniz2.sourceforge.net/safeeval.html[^].

—SA
 
Share this answer
 
Comments
kishore@cse 22-Aug-14 2:17am    
Is it possible in another languages like C,Java?
Sergey Alexandrovich Kryukov 22-Aug-14 2:29am    
Please see Solution 2.
—SA
kishore asked:
Is it possible in another languages like C, Java?
Interesting question.
Basically, no. Implementation of such thing in compiler languages is generally impossible, by apparent reasons: the function you may want to use needs to be compilers, as well as its call.

However, in certain technologies, something analogous is possible, but, technically, this is a very different thing, related to compilation.

For example, .NET supports compilation during runtime, and the framework guarantees the presence of some compilers. But the solution is conceptually very different: you compile some code with all the attributes of complete project into an assembly, then load this assembly, finds the implemented interface and instantiate some implementing class using reflection, and than use this instance to execute some code, even in the same process which you used in the compilation in first place. But the problems are only started here: if you want to do this over and over (in the same process), you will have a memory leak, because it's impossible to unload a loaded assembly. The solution is to have a separate application domain in the same process, and each time create it and delete the whole domain. But then you have to work through the domain boundary, which is not so easy, because the domains are isolated, so you would need to use IPC. This way, you can create a "compiling calculator" for anything. This is a very interesting stuff, I've done all that, and more. But, as you can see, this is very, very different from the approach of the interpretive languages.

—SA
 
Share this answer
 
v3
In fact,you can archieve this using reflection in c#. Something like:

C#
Type thisType = this.GetType();
MethodInfo theMethod = thisType.GetMethod(TheMethodString);
theMethod.Invoke(this, userParameters);


But you have to take care of what SA explained very well in his answer
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900