Zo Has
Zo Has

Reputation: 13018

Specify & fetch Integer value in Enum

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
    }

Upvotes: 0

Views: 116

Answers (1)

Oded
Oded

Reputation: 498904

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.

Upvotes: 3

Related Questions