5,448,416 members and growing! (19,518 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Mapping Text to Enum entries

By Reto Ravasio

Attaching a description to each entry in an enum.
C#, Windows, .NET 1.0, .NET 1.1, .NETVisual Studio, VS.NET2002, Dev

Posted: 6 Jun 2003
Updated: 6 Jun 2003
Views: 106,229
Bookmarked: 64 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
54 votes for this Article.
Popularity: 8.08 Rating: 4.67 out of 5
3 votes, 5.6%
1
1 vote, 1.9%
2
0 votes, 0.0%
3
9 votes, 16.7%
4
41 votes, 75.9%
5

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



Location: Switzerland Switzerland

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 24 of 24 (Total in Forum: 24) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralAwesome - thanks!member9356:48 22 Jun '07  
GeneralDifferent versions for speed improvementsmemberclementgatin12:37 12 Mar '07  
GeneralThanks againsupporterMarc Clifton4:39 15 Oct '06  
GeneralSolution for C / C++memberJimmyO10:56 5 May '05  
GeneralRe: Solution for C / C++sitebuilderUwe Keim20:53 10 Apr '06  
Generalwhy???memberkoo911:53 18 Jan '05  
GeneralRe: why???memberReto Ravasio9:36 19 Jan '05  
GeneralWhat about Flags?memberSpliffy735:22 22 Mar '04  
AnswerRe: What about Flags?memberJSpens8:50 14 Aug '07  
GeneralAutoDocsussDave Miller18:50 4 Feb '04  
GeneralI liked thismemberPalladino3:39 26 Aug '03  
GeneralWhat About using the System Enummembervbnetuk23:10 25 Jun '03  
GeneralRe: What About using the System EnummemberReto Ravasio2:04 4 Jul '03  
GeneralRe: What About using the System Enummembervbnetuk22:05 6 Jul '03  
GeneralRe: What About using the System EnummemberReto Ravasio5:47 7 Jul '03  
GeneralRe: What About using the System Enummemberdevaulr3:40 6 Nov '03  
GeneralRe: What About using the System Enummemberdatavalue4:06 19 Oct '05  
GeneralRe: What About using the System EnummemberConradC9:22 1 Feb '05  
GeneralMultilingualsitebuilderUwe Keim22:36 7 Jun '03  
GeneralRe: MultilingualeditorHeath Stewart6:07 27 Oct '03  
GeneralRe: MultilingualsitebuilderUwe Keim20:28 7 Mar '05  
GeneralRe: MultilingualmemberCraig Fisher10:18 30 Oct '07  
GeneralThe descriptions will also show up in the property gridmemberleppie11:07 7 Jun '03  
GeneralRe: The descriptions will also show up in the property gridmemberReto Ravasio0:34 8 Jun '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Jun 2003
Editor: Smitha Vijayan
Copyright 2003 by Reto Ravasio
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project