Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Enumeration has fields that aren't userfriendly, possible to attached     meta data to them?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DotNetNewbie - 06 Mar 2008 04:59 GMT
Hi,

I am creating the administration side of a web application, and I want
to basically enumerate through a Enumerations items and display the
enum items name and value.

Problem is the name of each item is not user friendly, is it possibel
for me to attach some kind of meta deta that I could access via
reflection?

example:

public enum SomeEnum
{
       BrownColor   = 1,
       GreenColor   = 2
}

Since 'browncolor' and 'greencolor' is not written properly (I would
like "brown color"), is there any kind of meta data I could add?

Like:

public enum SomeEnum
{
        ["Brown Color"]
        BrownColr = 2

}

Possible?
Peter Duniho - 06 Mar 2008 07:22 GMT
> [...]
> public enum SomeEnum
[quoted text clipped - 5 lines]
> Since 'browncolor' and 'greencolor' is not written properly (I would
> like "brown color"), is there any kind of meta data I could add?

I don't know if there's an existing attribute you could apply.  There  
might be.  But if not, you could always define your own attribute class.

That said, depending on what naming convention is being followed in the  
enum, maybe it's enough to just insert spaces before capital letters?  
That wouldn't require any changes to the type itself...you'd just scan the  
value name and when you reach a character for which converting to  
lower-case returns a different character than the actual character, stick  
a space into the displayed string before that character.

Pete
Jon Skeet [C# MVP] - 06 Mar 2008 07:46 GMT
> > Since 'browncolor' and 'greencolor' is not written properly (I would
> > like "brown color"), is there any kind of meta data I could add?
>
> I don't know if there's an existing attribute you could apply.  There
> might be.  But if not, you could always define your own attribute class.

I've used the Description attribute before -
System.ComponentModel.DescriptionAttribute.

<snip>

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

DotNetNewbie - 06 Mar 2008 22:11 GMT
> > > Since 'browncolor' and 'greencolor' is not written properly (I would
> > > like "brown color"), is there any kind of meta data I could add?
[quoted text clipped - 10 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

So say you have an enumerator, how could I loop through all the items
and output the Description attribute for each item?
Jon Skeet [C# MVP] - 06 Mar 2008 23:15 GMT
> So say you have an enumerator, how could I loop through all the items
> and output the Description attribute for each item?

There may well be a better way of doing it, but this certainly works:

using System;
using System.ComponentModel;
using System.Reflection;

enum Colour
{
   [Description("Ruby")]
   Red,
   [Description("Emerald")]
   Green,
   [Description("Sapphire")]
   Blue
}

class Test
{
   static void Main()
   {
       foreach (Colour colour in Enum.GetValues(typeof(Colour)))
       {
           FieldInfo field = typeof(Colour).GetField
               (colour.ToString());
           
           DescriptionAttribute description = (DescriptionAttribute)
               Attribute.GetCustomAttribute
                   (field, typeof(DescriptionAttribute));
           
           Console.WriteLine("{0}={1}",
                             colour,
                             description.Description);
       }
   }
}

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.