Beginner: Howto Declare variable using string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to use a string variable to declare a variable.
How would I accomplish this goal?

string nameMainMenu = "mnuMain";
string nameMenuItem = "mnuFile";

MainMenu {nameMainMenu} = new MainMenu();
this.Menu = {nameMainMenu};
MenuItem {nameMenuItem} = new MenuItem("&File");
{nameMainMenu}.MenuItems.Add({nameMenuItem});
 
Jeff,

Why would you need this in the first place? Wouldn't a Hashtable be better
for matching menu items to their names?

Anyway, you cannot declare varibales the way you want it. You can however
access variables by their names by using Reflection, still the code would be
much more complicated than that you have imagined.
 
Jeff Higgins said:
I would like to use a string variable to declare a variable.
How would I accomplish this goal?

string nameMainMenu = "mnuMain";
string nameMenuItem = "mnuFile";

MainMenu {nameMainMenu} = new MainMenu();
this.Menu = {nameMainMenu};
MenuItem {nameMenuItem} = new MenuItem("&File");
{nameMainMenu}.MenuItems.Add({nameMenuItem});

You can't. Why do you want to? I'm sure there's a way of achieving the
result you need, but not in the manner you were expecting.
 
Back
Top