19

How can I get Enum description from its value?

I can get the description from the name using:

Public Shared Function GetEnumDescription(ByVal EnumConstant As [Enum]) As String
    Dim fi As FieldInfo = EnumConstant.GetType().GetField(EnumConstant.ToString())
    Dim attr() As DescriptionAttribute = _ 
                  DirectCast(fi.GetCustomAttributes(GetType(DescriptionAttribute), _
                  False), DescriptionAttribute())

    If attr.Length > 0 Then
        Return attr(0).Description
    Else
        Return EnumConstant.ToString()
    End If
End Function 

But I cant figure out how to pass a variable name to this function. I've tried things like

GetEnumDescription([Enum].GetName(GetType(myEnum), 2)))

but nothing I've tried is correct.

3 Answers 3

20

If you have a variable of your enum type, it's simply

GetEnumDescription(myEnum)

Minimal working example:

Enum TestEnum
    <Description("Description of Value1")>
    Value1
End Enum

Public Sub Main()
    Dim myEnum As TestEnum = TestEnum.Value1
    Console.WriteLine(GetEnumDescription(myEnum)) ' prints "Description of Value1"
    Console.ReadLine()
End Sub

If you have an Integer variable, you need to cast it to your enum type first (CType works as well):

GetEnumDescription(DirectCast(myEnumValue, TestEnum))

Working example:

Enum TestEnum
    <Description("Description of Value1")>
    Value1 = 1
End Enum

Public Sub Main()
    Console.WriteLine(GetEnumDescription(DirectCast(1, TestEnum)))
    Console.ReadLine()
End Sub

The source for your confusion seems to be a misunderstanding: Your method does not take the "name" of an enum as a parameter, it takes an Enum as a parameter. That's something different, and it's also the reason why your attempts to use GetName failed.

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

5 Comments

Tried that but it didn't work... it only works with MyEnum.name but I don't have the name, only the value.
@doovers: Updated my answer. You simply need a cast.
thanks man I knew it would be simple but I couldn't figure it out!
GetEnumDescription doesn't exist for me. I had to add it using an extension as shown in the answer provided by @mikro
@tmighty: It's not a built-in, it's a method written by the OP and can be found in the question itself.
7

Here's another solution to get an Enum's description as an extension.

Imports System.ComponentModel
Imports System.Runtime.CompilerServices

<Extension()> Public Function GetEnumDescription(ByVal EnumConstant As [Enum]) As String
    Dim attr() As DescriptionAttribute = DirectCast(EnumConstant.GetType().GetField(EnumConstant.ToString()).GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())
    Return If(attr.Length > 0, attr(0).Description, EnumConstant.ToString)
End Function

Example use from the previous posts:

Enum Example
    <Description("Value1 description.")> Value1 = 1
    <Description("Value2 description.")> Value2 = 2
End Enum

Sub Main()
    Console.WriteLine(DirectCast(2, Example).GetEnumDescription())
    Console.ReadLine()
End Sub

Comments

0
<Extension()> Public Function GetEnumDescription(ByVal EnumConstant As [Enum]) As String
    Dim attr() As DescriptionAttribute = DirectCast(EnumConstant.GetType().GetField(EnumConstant.ToString()).GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())
    Return If(attr.Length > 0, attr(0).Description, EnumConstant.ToString)
End Function

when 
  Console.WriteLine(DirectCast(3, Example).GetEnumDescription())
report error

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.