473,385 Members | 1,736 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 execute a method from user input

Dear Group,
i have a combobox and a button on a form.
the combobox has three values: stop, pause,start
when user press the button, the program will take apropriate action
at the momment, here is what i did
switch (choice)
{
case start: objGame.start
case stop: objGame.stop
case pause: objGame.pause
}

what i really want to be able to do is : (as methods may expand in the
future like restart, ...)
doaction(userchoice)

void doaction(string userchoice)
{
string str = "objGame."+userchoice;
//How to this
run str
}

TIA
L.Peter
Nov 15 '05 #1
3 1233
The way you did it to start with is fine.

"L.Peter" <no@spam.local> wrote in message
news:ux**************@TK2MSFTNGP11.phx.gbl...
Dear Group,
i have a combobox and a button on a form.
the combobox has three values: stop, pause,start
when user press the button, the program will take apropriate action
at the momment, here is what i did
switch (choice)
{
case start: objGame.start
case stop: objGame.stop
case pause: objGame.pause
}

what i really want to be able to do is : (as methods may expand in the
future like restart, ...)
doaction(userchoice)

void doaction(string userchoice)
{
string str = "objGame."+userchoice;
//How to this
run str
}

TIA
L.Peter

Nov 15 '05 #2
what you can do is
1) push the case statement into your objGame object
public void objGame.DoAction( SomeEnum choice)
{
switch(choise)
{
case SomeEnum.Start:
this.Start;
...
}

2) or, if you really, really, really want to do this, use reflection. It is
slow and overkill for this, but you can do it:

objGame.GetType().InvokeMember( choice.ToString(), BindingFlags.InvokeMethod
| BindingFlags.Public | BindingFlags.Instance, null, objGame, new object[]
{});

"L.Peter" <no@spam.local> wrote in message
news:ux**************@TK2MSFTNGP11.phx.gbl...
Dear Group,
i have a combobox and a button on a form.
the combobox has three values: stop, pause,start
when user press the button, the program will take apropriate action
at the momment, here is what i did
switch (choice)
{
case start: objGame.start
case stop: objGame.stop
case pause: objGame.pause
}

what i really want to be able to do is : (as methods may expand in the
future like restart, ...)
doaction(userchoice)

void doaction(string userchoice)
{
string str = "objGame."+userchoice;
//How to this
run str
}

TIA
L.Peter

Nov 15 '05 #3
Hi Philip,
I go with your first suggestion

Thank you very much

Regards

Peter

"Philip Rieck" <st***@mckraken.com> wrote in message
news:us**************@TK2MSFTNGP12.phx.gbl...
what you can do is
1) push the case statement into your objGame object
public void objGame.DoAction( SomeEnum choice)
{
switch(choise)
{
case SomeEnum.Start:
this.Start;
...
}

2) or, if you really, really, really want to do this, use reflection. It is slow and overkill for this, but you can do it:

objGame.GetType().InvokeMember( choice.ToString(), BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, objGame, new object[]
{});

"L.Peter" <no@spam.local> wrote in message
news:ux**************@TK2MSFTNGP11.phx.gbl...
Dear Group,
i have a combobox and a button on a form.
the combobox has three values: stop, pause,start
when user press the button, the program will take apropriate action
at the momment, here is what i did
switch (choice)
{
case start: objGame.start
case stop: objGame.stop
case pause: objGame.pause
}

what i really want to be able to do is : (as methods may expand in the
future like restart, ...)
doaction(userchoice)

void doaction(string userchoice)
{
string str = "objGame."+userchoice;
//How to this
run str
}

TIA
L.Peter


Nov 15 '05 #4

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

Similar topics

37
by: Marek Kotowski | last post by:
I am looking for a tool to execute PHP scripts entered in a HTML form. For example: user is given a problem to solve. He writes a PHP script, sends it to a server, where it is executed and...
24
by: el_roachmeister | last post by:
Is there a way to make a text link post to a form without passing all the parameters in the url? The urls tend to get very long and messy. I often wonder if there is a limit to how long they can...
4
by: Thomas Scheiderich | last post by:
Why would you use the Recordset object over the Execute method of getting data from your Sql database. For example, I have the following: Execute Method...
4
by: Fred | last post by:
I have read and tried every combination for hours. I'm sorry, but I don't get it. How do I have a form with textboxes and buttons and allow click events to execute a server function then display...
1
by: Mark Valk | last post by:
I am having trouble with a line of code from a database currently in progress. I swear it worked yesterday but today I get a "too few parameters, 1 expected" error 3061 message. I have an...
1
by: Jack Bauer | last post by:
How can I execute a query which prompt the user for input? Something like SELECT FROM Table WHERE Name= In Oracle PL/SQL, you use & before variable names to have the SQL*Plus ask for input....
3
by: elrondrules | last post by:
Hi I am pretty new to PHP and hence need your help!! I need to write a PHP that shows the user a button (button1) and a series of check boxes and another button (button2).. If the button1...
1
by: anbumozhip | last post by:
i have the following error when i trying to execute the page Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\test\ouraction.php on line 14 ...
3
by: RAG2007 | last post by:
I'm using the QueryDef and Execute method to update a record in my MySQL backend. Problem: When the Passthrough update query is defined using QueryDef, it becomes a select query, and I cannot use...
5
by: rmurgia | last post by:
I set up an Access database with a text field to record the website name and a date field to record the date/time of the access. I have set up a page with various anchors set up to call up different...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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
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
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...

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.