Click here to Skip to main content
15,909,590 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi - ITNOG

I want to calling functions by user input , for example if user entered 'max' , max function will call , like it :


int max ( int k,int z)
{
...
}


int min( int k,int z)
{
...
}
void Main()
{

string s;

consol.read(s);


//What should I do now 
...

consol.write("You have entered max , so max function called and it returned 5 ");

}





I want something that get function name , and run it .

without using if.
Posted
Comments
[no name] 30-Jun-10 10:40am    
You will fire an event based on the user input…..So at first you must make sure that the input is valid and then you take action…. I don’t’ understand why / how you can make sure on what action you need to execute.

c# or c++? because the options available may not be the same.

If you don't want to use an if, how about a switch?

Though I suppose if you don't want an if, you don't want a switch.

This page: C# FAQ: How call a method using a name string[^]) gives some code that calls a method by name using MethodInfo.

To pass multiple params, you'd want something like:
C#
methodInfo.Invoke(chosenMethod, new object[] {k, z});


But that's c#.
 
Share this answer
 
This is something not easy at all to do in C++ (well, in a nice and flexible way at least). Are you sure that you want to do something like that ? What is the reason behind that ?
If you really want to do something like that, I would integrate a script language inside your C++ app.

In your example, you not only need to give the name of the function name but the user has to also provides the parameters for the function. Imagine when you have a lot of different functions, with different parameter types and number, you'll need to make sure everything is correct.

If I had to do something like that, I would do it this way: I wouldn't have the different functions but each "function" would in fact be a class inheriting from a base class (something like CBaseOperation). The base class has virtual functions to ask for the parameters and to execute the operation. Each sub-class would then implement those functions in order to ask for the correct number of parameters and check their type. Then they also implement the function to execute the operation.

Finally, all these classes are registered in a factory class (which is just a wrapper around a map, with the operation name being the key and an instance of the class being the value). When the user enters the function name, the program looks in the map to see if the object exists and call the askParameters function then the executeOperation function (or whatever names).

This is I think one elegant and flexible way to solve your problem, but are you really sure that it is what you want to do ?
 
Share this answer
 
You're going to have to look into reflection and possibly expressions(that is a field I'm not an expert in) to be able to do this. You're not going to be able to avoid using 'if' somewhere along the line, not if you want the code to be pretty robust.

typeof( Program).GetMethod( "Max").Invoke( .. )[read the docs on the rest] is what I think you're looking for
 
Share this answer
 
Comments
William Winner 30-Jun-10 10:39am    
I would first make sure that the method exists. If the user typed, "Jiehjfksidjkfdiw" and you tried to invoke it straight, you'd get an error.
hammerstein05 30-Jun-10 11:14am    
Agreed. I was trying to do the "pointing the poster in the right direction" thing rather than hand holding all the way through.
William Winner 6-Jul-10 18:34pm    
Yeah, I was just trying to build on your answer...since I didn't mention it either and you beat me to it!
If you like to experiment a bit, have a look at Lua [^].
:)
 
Share this answer
 

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