0

How can I get my enum to contain numbers for string descriptions? I have used [Description] for text strings. I want something like

  Apple pie=1
  Strawberry pie=2

My database would carry the numeric fields & I will fetch text description from the enum. This does not work

public enum Pies
    {
        [Description("Apple pie")]
        "1"=1,
        [Description("Strawberry pie")]
        "0"=0
    }
ssilas777
  • 9,672
  • 4
  • 45
  • 68
Zo Has
  • 12,599
  • 22
  • 87
  • 149

1 Answers1

3

First, declare a valid enum:

public enum Pies
{
    [Description("Apple pie")]
    Apple =1,
    [Description("Strawberry pie")]
    Strawberry=0
}

You can use reflection to access the DescriptionAttribute.

Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Thanks,I am successul in accessing the description, I just want to know how to include numeric fields instead of Apple & strawberry in the enum as the database would return me numbers & by using them I need to fetch the text description. Say if user selects Apple Pie in my dropdown I need to have 1 as its value. – Zo Has Sep 11 '12 at 04:57
  • 1
    @DamienJoe - How are you populating the dropdown? If you set the _value_ of the field to the enum value, you just need to _cast_ `(Pies)1`, for example. – Oded Sep 11 '12 at 08:36