Click here to Skip to main content
11,493,171 members (63,596 online)
Click here to Skip to main content

Mapping Text to Enum entries

, 6 Jun 2003 176.6K 1.8K 89
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Reto Ravasio

Switzerland Switzerland
No Biography provided

Comments and Discussions

 
QuestionWhat About using the System Enum Pin
vbnetuk25-Jun-03 23:10
membervbnetuk25-Jun-03 23:10 
AnswerRe: What About using the System Enum Pin
Reto Ravasio4-Jul-03 2:04
memberReto Ravasio4-Jul-03 2:04 
GeneralRe: What About using the System Enum Pin
vbnetuk6-Jul-03 22:05
membervbnetuk6-Jul-03 22:05 
Hi
I didnt know that

Are you saying that using the system enum is not good if you are using long description or if the enum contains characters that are in the enum>?

Thanks


Thanks A lot
Gabriel
vbnetuk@yahoo.co.uk
GeneralRe: What About using the System Enum Pin
Reto Ravasio7-Jul-03 5:47
memberReto Ravasio7-Jul-03 5:47 
GeneralRe: What About using the System Enum Pin
devaulr6-Nov-03 3:40
memberdevaulr6-Nov-03 3:40 
GeneralRe: What About using the System Enum Pin
datavalue19-Oct-05 4:06
memberdatavalue19-Oct-05 4:06 
AnswerRe: What About using the System Enum Pin
ConradC1-Feb-05 9:22
memberConradC1-Feb-05 9:22 

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.

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