0

I have an enum and the list has to items per value. I would like to be able to display the enum list to end user in a textbox using the string. Here is my enum:

enum CMD
{        
    COMMANDS, CMD = 0,
    ONE,      O =   1,
    TWO,      T =   2,
    THREE,    TH =  3,
    FOUR,     F =   4,        
    FIVE,     FI =  5,
    SIX,      S =   6,
    SEVEN,    SE =  7     
}

Here is what I have tried to accomplish this with the following code, using to value of the enum to display both valued list items:

CMD cmd = (CMD)Enum.Parse(typeof(CMD), input);
if (cmd == CMD.COMMANDS)
{
    sb.Clear();
    sb.AppendLine("List the commands:");                    
    sb.AppendLine(((CMD)0).ToString() + "," + ((CMD)0).ToString("F").Split(',').Last());
    sb.AppendLine(((CMD)1).ToString() + "," + ((CMD)1).ToString("F").Split(',').Last());
    sb.AppendLine(((CMD)2).ToString() + "," + ((CMD)2).ToString("F").Split(',').Last());
    sb.AppendLine(((CMD)3).ToString() + "," + ((CMD)3).ToString("F").Split(',').Last());
    sb.AppendLine(((CMD)4).ToString() + "," + ((CMD)4).ToString("F").Split(',').Last());
    sb.AppendLine(((CMD)5).ToString() + "," + ((CMD)5).ToString("F").Split(',').Last());
    sb.AppendLine(((CMD)6).ToString() + "," + ((CMD)6).ToString("F").Split(',').Last());
    sb.AppendLine(((CMD)7).ToString() + "," + ((CMD)7).ToString("F").Split(',').Last());
    textbox.Text = sb.ToString();
}

The output I get is:

List the commands:
CMD,COMMANDS
O, O
T, T
TH, TH
F, F
FI, FI
S, S
SEVEN, SE

I want to output to be like this:

        COMMANDS, CMD
        ONE,   O
        TWO,   T
        THREE, TH
        FOUR,  F        
        FIVE,  FI
        SIX,   S
        SEVEN, SE   

How can I accomplish the output like I want?

MethodMan
  • 18,625
  • 6
  • 34
  • 52
nate
  • 1,418
  • 5
  • 34
  • 73
  • See [How to get C# Enum description from value?](http://stackoverflow.com/q/2650080/1260204) – Igor Oct 05 '16 at 17:02
  • @Igor I looked at that already, I don't have a description – nate Oct 05 '16 at 17:03
  • 3
    The best option is to either use a description so you can be dynamic or use something else to come up with the nice spelling out of each option. To have 2 enum options per value only for display is bad design and you will run into problems. – Igor Oct 05 '16 at 17:08
  • @Igor Thanks for the idea, I am looking into it now – nate Oct 05 '16 at 17:12

3 Answers3

1

My guess is no, for the same reason that Enum.GetName is not deterministic:

From MSDN:

If multiple enumeration members have the same underlying value, the GetName method guarantees that it will return the name of one of those enumeration members. However, it does not guarantee that it will always return the name of the same enumeration member. As a result, when multiple enumeration members have the same value, your application code should never depend on the method returning a particular member's name.

Enums are meant to replace a name with a numeric value, not the other way around, so when an enum defines multiple names that map to the same numeric value, there's not a way to specify which value you want.

Some options:

  • Add a [Description] attribute to your enum values instead of having duplicate names
  • Split your enum into two - one for the "long" name and one for the "short" name (note that you'd have to choose one when defining input parameters)
  • Use something other than an enum.
D Stanley
  • 149,601
  • 11
  • 178
  • 240
1

How about something like the following:

        var dictonary = new Dictionary<CMD, List<string>>();
        var names = Enum.GetNames(typeof(CMD));
        foreach (var name in names)
        {
            var value = (CMD)Enum.Parse(typeof(CMD), name);
            if(!dictonary.ContainsKey(value))
                dictonary.Add(value, new List<string>() {name});
            else
                dictonary[value].Add(name);
        }

This way you have a dictionary of the enum descriptions with the values and you can implement the result as you wish.

TheRock
  • 1,513
  • 1
  • 18
  • 19
  • Looks promising, how would I implement the result into a string builder? I tried sb.AppendLine(dictonary.ToString()); – nate Oct 05 '16 at 17:57
  • dictonary.ToList().ForEach(x => sb.AppendLine(string.Join(",", x.Value))); – TheRock Oct 05 '16 at 18:19
0

The Enum class supports getting the Enum names using the GetNames method. This code doesn't take into account the abbreviated CMD names, because this is very uncommon.

string[] enumNames = Enum.GetNames(typeof(CMD)); // Get all the names and store in array 
textbox.Text = String.Join(System.Environment.NewLine, enumNames); // Convert array to new line separated strings
Bouke
  • 1,531
  • 1
  • 12
  • 21
  • That is close to what I want, it displays a drop down list of the commands. Anyway to make it look more like: COMMANDS, CMD – nate Oct 05 '16 at 17:34