Click here to Skip to main content
Licence 
First Posted 6 Jun 2003
Views 155,926
Bookmarked 86 times

Mapping Text to Enum entries

By | 6 Jun 2003 | Article
Attaching a description to each entry in an enum.

Sample Image - EnumWithDescription.png

Introduction

While porting old C code to C#, I stumbled across lots of huge arrays which mapped constants to strings. While looking for the C# way of doing this, I discovered custom attributes and reflection.

How is it done?

Append a Description attribute to each enum entry:

private enum MyColors
{
   [Description("yuk!")]       LightGreen    = 0x012020,
   [Description("nice :-)")]   VeryDeepPink  = 0x123456,
   [Description("so what")]    InvisibleGray = 0x456730,
   [Description("no comment")] DeepestRed    = 0xfafafa,
   [Description("I give up")]  PitchBlack    = 0xffffff,
}

To access the description from code, the following function is called:

public static string GetDescription(Enum value)
{
   FieldInfo fi= value.GetType().GetField(value.ToString()); 
   DescriptionAttribute[] attributes = 
         (DescriptionAttribute[])fi.GetCustomAttributes(
         typeof(DescriptionAttribute), false);
   return (attributes.Length>0)?attributes[0].Description:value.ToString();
}

Without the following using declaration, the code will not compile.

using System.ComponentModel;        
using System.Reflection;

Benefits

Instead of having constants and huge arrays all over the code, the above solution keeps declarations and descriptions nicely together.

History

I've only found out by accident that the framework already provides a DescriptionAttribute class.

I built the first version with my own custom attribute and got conflicts when I started using the ComponentModel namespace.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Reto Ravasio



Switzerland Switzerland

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberlendres7:14 3 Apr '12  
GeneralMy vote of 5 PinmemberDorofejev23:44 19 Dec '11  
QuestionHow to use with dataset / tableadapter PinmemberSaelen Kenny0:01 8 Nov '09  
QuestionQuery regarding description Tag in Enum PinmemberNITINKADAM5:14 24 Nov '08  
GeneralAwesome - thanks! Pinmember9355:48 22 Jun '07  
GeneralDifferent versions for speed improvements Pinmemberclementgatin11:37 12 Mar '07  
GeneralThanks again PinprotectorMarc Clifton3:39 15 Oct '06  
GeneralSolution for C / C++ PinmemberJimmyO9:56 5 May '05  
GeneralRe: Solution for C / C++ PinsitebuilderUwe Keim19:53 10 Apr '06  
Questionwhy??? Pinmemberkoo910:53 18 Jan '05  
AnswerRe: why??? PinmemberReto Ravasio8:36 19 Jan '05  
QuestionWhat about Flags? PinmemberSpliffy734:22 22 Mar '04  
AnswerRe: What about Flags? PinmemberJSpens7:50 14 Aug '07  
AnswerRe: What about Flags? Pinmemberfrinkfree12:55 11 Nov '08  
GeneralAutoDoc PinsussDave Miller17:50 4 Feb '04  
GeneralI liked this PinmemberPalladino2:39 26 Aug '03  
QuestionWhat About using the System Enum Pinmembervbnetuk22:10 25 Jun '03  
AnswerRe: What About using the System Enum PinmemberReto Ravasio1:04 4 Jul '03  
GeneralRe: What About using the System Enum Pinmembervbnetuk21:05 6 Jul '03  
GeneralRe: What About using the System Enum PinmemberReto Ravasio4:47 7 Jul '03  
GeneralRe: What About using the System Enum Pinmemberdevaulr2:40 6 Nov '03  
GeneralRe: What About using the System Enum Pinmemberdatavalue3:06 19 Oct '05  
AnswerRe: What About using the System Enum PinmemberConradC8:22 1 Feb '05  
GeneralMultilingual PinsitebuilderUwe Keim21:36 7 Jun '03  
GeneralRe: Multilingual PineditorHeath Stewart5:07 27 Oct '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 7 Jun 2003
Article Copyright 2003 by Reto Ravasio
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid