How to store an action (i.e. Method to call later). Also i18n

Posted on 2013-09-30
Medium Priority
Last Modified: 2013-10-02
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?

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.
Question by:deleyd
  • 2
  • 2
LVL 86

Accepted Solution

jkr earned 2000 total points
ID: 39534141
>>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

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")

Author Comment

ID: 39536889
Thank you. That answers the C++ part of my question. How about the rest? Anyone have comments on the rest?
LVL 86

Expert Comment

ID: 39537040
Sorry, can't help you with the C# part...

Author Closing Comment

ID: 39540307
Thank you for answering C++ part. Reposted to get C# export.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question