Click here to Skip to main content
Click here to Skip to main content

Mapping Text to Enum entries

By , 6 Jun 2003
 

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
No Biography provided

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberlendres3-Apr-12 7:14 
GeneralMy vote of 5memberDorofejev19-Dec-11 23:44 
QuestionHow to use with dataset / tableadaptermemberSaelen Kenny8-Nov-09 0:01 
QuestionQuery regarding description Tag in EnummemberNITINKADAM24-Nov-08 5:14 
GeneralAwesome - thanks!member93522-Jun-07 5:48 
GeneralDifferent versions for speed improvementsmemberclementgatin12-Mar-07 11:37 
GeneralThanks againprotectorMarc Clifton15-Oct-06 3:39 
GeneralSolution for C / C++memberJimmyO5-May-05 9:56 
GeneralRe: Solution for C / C++sitebuilderUwe Keim10-Apr-06 19:53 
Questionwhy???memberkoo918-Jan-05 10:53 
AnswerRe: why???memberReto Ravasio19-Jan-05 8:36 
QuestionWhat about Flags?memberSpliffy7322-Mar-04 4:22 
AnswerRe: What about Flags?memberJSpens14-Aug-07 7:50 
AnswerRe: What about Flags?memberfrinkfree11-Nov-08 12:55 
GeneralAutoDocsussDave Miller4-Feb-04 17:50 
GeneralI liked thismemberPalladino26-Aug-03 2:39 
QuestionWhat About using the System Enummembervbnetuk25-Jun-03 22:10 
AnswerRe: What About using the System EnummemberReto Ravasio4-Jul-03 1:04 
GeneralRe: What About using the System Enummembervbnetuk6-Jul-03 21:05 
GeneralRe: What About using the System EnummemberReto Ravasio7-Jul-03 4:47 
GeneralRe: What About using the System Enummemberdevaulr6-Nov-03 2:40 
GeneralRe: What About using the System Enummemberdatavalue19-Oct-05 3:06 
AnswerRe: What About using the System EnummemberConradC1-Feb-05 8:22 
They are two different things if I understand your message correctly. Just like property name and property description are two different attributes of a property.
 
Enum name is more useful for developers and compilers. It is not practically useful for display to end user since it has all the naming limits and is not localizable.
 
Just like the other comment from other thread, it would be nice to have this attribute to use resources instead of hard coded string. Also performance would be a concern since it uses reflection alot. How about caching the result from GetDescription static method?
 
Just my 2 cents
Conrad:->
GeneralMultilingualsitebuilderUwe Keim7-Jun-03 21:36 
GeneralRe: MultilingualeditorHeath Stewart27-Oct-03 5:07 
GeneralRe: MultilingualsitebuilderUwe Keim7-Mar-05 19:28 
GeneralRe: MultilingualmemberCraig Fisher30-Oct-07 9:18 
GeneralThe descriptions will also show up in the property gridmemberleppie7-Jun-03 10:07 
GeneralRe: The descriptions will also show up in the property gridmemberReto Ravasio7-Jun-03 23:34 

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

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