deleyd
asked on
How to store an action (i.e. Method to call later). Also i18n
I'm creating a test program in C#. (The final program will be in C++, but I'm more familiar with C#) Right now I'm just composing a general idea of how this program will be structured. This will be a menu program.
A menu has a list of menuItems.
Each menuItem has some menu Text to display to the user, and an Action which is what the program does if the user executes that menuItem.
The Menu also keeps track of the currently selected menu item currentMenuItem.
Will I end up with a separate Delegate for each possible Action?
Perhaps a Lambda expression will work OK.
The Menu Item Action may be "go to submenu Xyzzy". So MenuItem will be referring to sub menu Xyzzy, and I start having tightly coupled code. How do I avoid tightly coupled code?
Also I'd like to i18n the menu Text so I'm not hard coding English into my code. Would like to make it flexible so the menu can be displayed in other languages.
The two methods I've seen use Dictionary lookups, either create an enum variable for each message string and look that up in the appropriate language dictionary (e.g. DoThis, DoThat, DoSomethingElse, becomes "Do This", "Do that", "Do something else"). The problem with this is you end up with numerous enum variables that are either cryptic or very long.
Or, I've also seen using English text as the key instead of enum variables (e.g. "Do this", "Do that", "Do something else" for English becomes "Do this", "Do that", "Do something else", and for French becomes a different set of strings.) Problem with this is if you want to change the English, you might change the code but forget to change the dictionary.
Not sure what a good way to go is for this i18n thing.
A menu has a list of menuItems.
Each menuItem has some menu Text to display to the user, and an Action which is what the program does if the user executes that menuItem.
The Menu also keeps track of the currently selected menu item currentMenuItem.
Class Menu
{
has list of MenuItems
maintains currentMenuItem (the menu item currently selected)
Method OnUpArrow
set currentMenuItem to the previous one
Method OnDownArrow
set currentMenuItem to the next one
Method OnDoMenuItemAction
run the method which is what this menu item does
currentMenuItem.Execute
}
Class MenuItem
{
property menu text
property menu action //run this if user executes this menu item
Method Execute
run action
MenuItem(string menuText, thing to do) //CONSTRUCTOR
text = menuText;
action = thing to do
}
main program
a Stack of Menus
Method PushMenu(Menu menu)
push this menu onto the stack
Method PopMenu(Menu menu)
pop top menu off the stack and return it
OnEventUserSelectsMenuItemAction
topMenuOnStack.currentMenuItem.Action();
OnEnterKey
call OnEventUserSelectsMenuItemAction
}
My question is how do I deal with storing the MenuItem Action? I'm thinking Delegates but don't quite have the syntax right. Will I end up with a separate Delegate for each possible Action?
Perhaps a Lambda expression will work OK.
The Menu Item Action may be "go to submenu Xyzzy". So MenuItem will be referring to sub menu Xyzzy, and I start having tightly coupled code. How do I avoid tightly coupled code?
Also I'd like to i18n the menu Text so I'm not hard coding English into my code. Would like to make it flexible so the menu can be displayed in other languages.
The two methods I've seen use Dictionary lookups, either create an enum variable for each message string and look that up in the appropriate language dictionary (e.g. DoThis, DoThat, DoSomethingElse, becomes "Do This", "Do that", "Do something else"). The problem with this is you end up with numerous enum variables that are either cryptic or very long.
Or, I've also seen using English text as the key instead of enum variables (e.g. "Do this", "Do that", "Do something else" for English becomes "Do this", "Do that", "Do something else", and for French becomes a different set of strings.) Problem with this is if you want to change the English, you might change the code but forget to change the dictionary.
Not sure what a good way to go is for this i18n thing.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.