4

Can I use enum like below?

I can do it like TeamManager, TeamLeader, SeniorDeveloper etc. But I want to give a space between words like "Team Manager"

public enum SoftwareEmployee
{
    Team Manager = 1,
    Team Leader = 2,
    Senior Developer = 3,
    Junior = 4
}
Stack User
  • 1,378
  • 5
  • 19
  • 40

3 Answers3

15

No, but you can do this instead:

public enum SoftwareEmployee {
    [Description("Team Manager")] TeamManager = 1,
    [Description("Team Leader")] TeamLeader = 2,
    [Description("Senior Developer")] SeniorDeveloper = 3,
    [Description("Junior")] Junior = 4
}

You can then use a utility method to translate enum values to descriptions, for example:

    /// <summary>
    /// Given an enum value, if a <see cref="DescriptionAttribute"/> attribute has been defined on it, then return that.
    /// Otherwise return the enum name.
    /// </summary>
    /// <typeparam name="T">Enum type to look in</typeparam>
    /// <param name="value">Enum value</param>
    /// <returns>Description or name</returns>
    public static string ToDescription<T>(this T value) where T : struct {
        if(!typeof(T).IsEnum) {
            throw new ArgumentException(Properties.Resources.TypeIsNotAnEnum, "T");
        }
        var fieldName = Enum.GetName(typeof(T), value);
        if(fieldName == null) {
            return string.Empty;
        }
        var fieldInfo = typeof(T).GetField(fieldName, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static);
        if(fieldInfo == null) {
            return string.Empty;
        }
        var descriptionAttribute = (DescriptionAttribute) fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault();
        if(descriptionAttribute == null) {
            return fieldInfo.Name;
        }
        return descriptionAttribute.Description;
    }

I prefer this over manual translation via switch, because it is easier to maintain the enum definitions if everything is together.

To allow localisation of the description text, use a different description attribute that takes its value from a resource, e.g. ResourceDescription. As long as it inherits from Description, then it will work fine. For example:

public enum SoftwareEmployee {
    [ResourceDescription(typeof(SoftwareEmployee), Properties.Resources.TeamManager)] TeamManager = 1,
    [ResourceDescription(typeof(SoftwareEmployee), Properties.Resources.TeamLeader)] TeamLeader = 2,
    [ResourceDescription(typeof(SoftwareEmployee), Properties.Resources.SeniorDeveloper)] SeniorDeveloper = 3,
    [ResourceDescription(typeof(SoftwareEmployee), Properties.Resources.Junior)] Junior = 4
}
Christian Hayter
  • 30,581
  • 6
  • 72
  • 99
  • While this works, it's not ideal in a scenario where the strings must be translated. The people working on translations like to have all the strings in one place, not scattered around the code... – Matthew Watson Apr 10 '13 at 08:11
  • possible duplicate of [Enum ToString](http://stackoverflow.com/questions/479410/enum-tostring) – Oliver Apr 10 '13 at 08:59
6

It is not possible to give a space as it would generate a compilation error. If you want it for displaying purposes. Then you could write a method that returns a UI friendly strings when passed enumeration

something like:

public string GetUIFriendlyString(SoftwareEmployee employee)
{
    switch (employee):
    {
      case TeamLeader: return "Team Leader";
      // same for the rest
    }
}

or use attributes in the enum as suggested by @Christian Hayter

Ali
  • 1,409
  • 13
  • 23
  • possible duplicate of [Enum ToString](http://stackoverflow.com/questions/479410/enum-tostring) – Oliver Apr 10 '13 at 08:58
1

No you cannot, this cannot be compiled.

You can put an underscore between the words Senior_Developer although you have to ask yourself am I writing code or am I writing an essay. Don't get me wrong code should be legible but it does not have to look like a sentence.

The only reason I can come to that you might want to do this is so that you can output it to the UI. Code such as enums or exceptions should not be out put to the UI, it can be useful to start with but you should use some sort of mapping to convert the enum to plain text. This is especially useful if you need to handle multiple locals.

Bronumski
  • 14,009
  • 6
  • 49
  • 77