34

How can I achieve the following using enums in .NET? I would like to have descriptions for each value that include spaces.

public enum PersonGender { Unknown = 0, Male = 1, Female = 2, Intersex = 3, Indeterminate = 3, Non Stated = 9, Inadequately Described = 9 } 

I would like to be able to choose whether to use either the description or integer each time I use a value of this type.

1

3 Answers 3

58

No that's not possible, but you can attach attributes to enum members. The EnumMemberAttribute is designed exactly for the purpose you described.

public enum PersonGender { Unknown = 0, Male = 1, Female = 2, Intersex = 3, Indeterminate = 3, [EnumMember(Value = "Not Stated")] NonStated = 9, [EnumMember(Value = "Inadequately Described")] InadequatelyDescribed = 9 } 

For more information on how to use the EnumMemberAttribute to convert strings to enum values, see this thread.

Sign up to request clarification or add additional context in comments.

9 Comments

Can a DescriptionAttribute be used?
Yes it can. In fact, any attribute class that has AttributeTargets.Field or AttributeTargets.All can be used. You can also write your own. Here are examples using DesciptionAttribute and a custom attribute.
+1 Very clear. I have a question though. I get The type or namespace name 'EnumMember' could not be found ... How does it get included in one's project? After a Google search, I found 3 results and none of them addressed the problem.
Oh sorry, I see, first I have to include System.Runtime.Serialization as a resource in my project and then put a using statement in the file
@bonCodigo For any class reference you find on MSDN, there's a namespace and assembly listed just above the syntax. The assembly is what you need to add to your project as a reference, and the namespace is what you need to add at the top of your file as a using directive. In this case, both are System.Runtime.Serialization
|
5

This is easy. Create an extension method for your string that returns a formatted string based on your coding convention. You can use it in lots of places, not just here. This one works for camelCase and TitleCase.

 public static String ToLabelFormat(this String s) { var newStr = Regex.Replace(s, "(?<=[A-Z])(?=[A-Z][a-z])", " "); newStr = Regex.Replace(newStr, "(?<=[^A-Z])(?=[A-Z])", " "); newStr = Regex.Replace(newStr, "(?<=[A-Za-z])(?=[^A-Za-z])", " "); return newStr; } 

Comments

1
var assembly = Assembly.LoadFrom("ResourcesLib.DLL"); var resourceManager = new ResourceManager("ResourcesLib.EnumDescriptions", assembly); var lst = Enum.GetValues(typeof(PersonGender)).Cast<PersonGender>().ToList(); foreach (var gender in lst) { Console.WriteLine(gender); // Name Console.WriteLine((int)gender); //Int Value Console.WriteLine(resourceManager.GetString(gender.ToString()));//localized Resorce } 

So spaces may reside in localized resource ...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.