5

How do i create aliases in c#

Take this scenario

class CommandMessages
{
   string IDS_SPEC1_COMPONENT1_MODULE1_STRING1;
}

say i create an object of this class

CommandMessages objCommandMessage = new CommandMessages();

To i need to write lengthy string

objCommandMessage.IDS_SPEC1_COMPONENT1_MODULE1_STRING1 

every time i access the variable, this is a pain as i am using this variable as a key for a dictionary.

Dict[objCommandMessage.IDS_SPEC1_COMPONENT1_MODULE1_STRING1]

therefore i should be able to do something like this

Dict[str1]

where str1 is alias for objCommandMessage.IDS_SPEC1_COMPONENT1_MODULE1_STRING1, How do i do it?

Gaddigesh
  • 1,953
  • 8
  • 30
  • 41

3 Answers3

8

Create another, shorter, property that references the original one?

class CommandMessages
{
    string IDS_SPEC1_COMPONENT1_MODULE1_STRING1;

    public string Str1
    {
        get
        {
            return this.IDS_SPEC1_COMPONENT1_MODULE1_STRING1;
        }
    }
}

Then you can use the following anywhere you like:

Dict[objCommandMessage.Str1]
Andy Shellam
  • 15,403
  • 1
  • 27
  • 41
3
string str1 = objCommandMessage.IDS_SPEC1_COMPONENT1_MODULE1_STRING1;
Rob Fonseca-Ensor
  • 15,510
  • 44
  • 57
2
public string str1 { get { return objCommandMessage.IDS_SPEC1_COMPONENT1_MODULE1_STRING1; } }
Michael Buen
  • 38,643
  • 9
  • 94
  • 118