Home > Software engineering >  I want to use string value as variable name, and the variable have to global variable
I want to use string value as variable name, and the variable have to global variable

Time:10-22

Searching in "stack overflow", I am coding below for string value as variable name.

var ESScommand = new Dictionary<string, ESS_command>();
ESScommand[sample.Data.ESS_name] = new ESS_command();
ESScommand[sample.Data.ESS_name] = sample.Data;

But I want to ESScommand[sample.Data.ESS_name] as global variable. Is it possible??

CodePudding user response:

Not as a global variable, but it is simple to make it a global property.

static class Globals
{
    public static ESS_command GlobalCommand => ESScommand[sample.Data.ESS_name];
}

To access:

var cmd = Globals.GlobalCommand;
  • Related