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
    }
0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.