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

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?

How will this work when I translate this to unmanaged C++ (which I'm not as familiar with the syntax)? Does unmanaged C++ just have pointers to functions, and you hope the pointer points correctly to the beginning of a function, because you're going to tell the computer to jump there and start executing what you hope is 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.
deleydSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
>>Does unmanaged C++ just have pointers to functions, and you hope the pointer points
>>correctly to the beginning of a function, because you're going to tell the computer to jump
>>there and start executing what you hope is code?

Actually, it's a lot better. You have
real

Open in new window

function or even method pointers that are guaranteed to represent valid entry points (well, if assigned correctly, of course). More on this issue at http://www.newty.de/fpt/index.html ("The Function Pointer Tutorials")
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deleydSoftware EngineerAuthor Commented:
Thank you. That answers the C++ part of my question. How about the rest? Anyone have comments on the rest?
0
jkrCommented:
Sorry, can't help you with the C# part...
0
deleydSoftware EngineerAuthor Commented:
Thank you for answering C++ part. Reposted to get C# export.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.