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.
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

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


    call OnEventUserSelectsMenuItemAction

Open in new window

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.
Who is Participating?
b_levittConnect With a Mentor Commented:
The framework already defines an Action delegate.  So if:
class MenuItem
  public Action Execute{get; set;}

Open in new window

var myMenuItem = new MenuItem(){Execute= () => MessageBox.Show("Hello World!")};

Open in new window

and then assuming your  menu item "clicked" or "selected" event passes the selected menu item (likey thru EventArgs but simplified here):
void MenuItemSelected(MenuItem selectedMenuItem)
  //then you would just need to invoke delegate on that menuitem:

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.