Jonathan Allen
Jonathan Allen

Reputation: 70307

.NET: What attribute would you use to describe an Enum value?

Lets say you have this enum:

Public Enum ApplicationStatus
    <Foo("Active")> Active = 1
    <Foo("Inactive")> Inactive = 2
    <Foo("Retired")> Retired = 3
    <Foo("In Development")> InDevelopment = 4
End Enum

What attribute should I use in place of Foo for plain-text descriptions of the Enum?

Upvotes: 1

Views: 115

Answers (3)

Paul Farry
Paul Farry

Reputation: 4768

I've used Description for mine, but only in the cases when the enumeration itself needs to display very differently, otherwise it just displays the String of the Enum Value.

I've also use a method for some where it checks the case of each character in the Enum item and if it's an uppercase character after the 1st, it adds a space before the character.

Public Enum ApplicationStatus
    Active = 1
    Inactive = 2
    Retired = 3
    InDevelopment = 4
    <Description("Radical Display Name")> Radical = 5 
End Enum

Upvotes: 1

Thomas Levesque
Thomas Levesque

Reputation: 292445

In addition to hunter's answer, you might want to check out this extension method to retrieve the description easily

Upvotes: 1

hunter
hunter

Reputation: 63522

I use Description()

[Description("The image associated with the control"),Category("Appearance")] 
 public Image MyImage {
    get {
       // Insert code here.
       return image1;
    }
    set {
       // Insert code here.
    }
 }

Upvotes: 3

Related Questions