I though this would be easy

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Dunaway

    #1

    I though this would be easy

    Given the following enum:

    Public Enum MyEnum
    <Description("F irst Item")> _
    FirstItem
    <Description("S econd Item")> _
    SecondItem
    <Description("T hird Item")> _
    ThirdItem
    End Enum

    If I have a variable of type MyEnum, how can I extract the value of
    the Description attribute (which is of type DescriptionAttr ibute)?

    Dim e As MyEnum = MyEnum.SecondIt em

    How can I get the value of the Description attribute (which is "Second
    Item")? Note that I do not want the enum name ("SecondItem "), I want
    the value of the attribute.

    I have tried the GetCustomAttrib utes member of System.Type, but that
    does not seem to work.

    Any hints??

    Thanks

  • Herfried K. Wagner [MVP]

    #2
    Re: I though this would be easy

    Chris,

    "Chris Dunaway" <dunawayc@gmail .com> schrieb:[color=blue]
    > Public Enum MyEnum
    > <Description("F irst Item")> _
    > FirstItem
    > <Description("S econd Item")> _
    > SecondItem
    > <Description("T hird Item")> _
    > ThirdItem
    > End Enum
    >
    > If I have a variable of type MyEnum, how can I extract the value of
    > the Description attribute (which is of type DescriptionAttr ibute)?
    >
    > Dim e As MyEnum = MyEnum.SecondIt em
    >
    > How can I get the value of the Description attribute (which is "Second
    > Item")? Note that I do not want the enum name ("SecondItem "), I want
    > the value of the attribute.[/color]

    Adding descriptions to enumeration constants
    <URL:http://dotnet.mvps.org/dotnet/faqs/?id=enumdescrip tion&lang=en>

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • Chris Dunaway

      #3
      Re: I though this would be easy

      Herfried K. Wagner [MVP] wrote:
      [color=blue][color=green]
      > > How can I get the value of the Description attribute (which is "Second
      > > Item")? Note that I do not want the enum name ("SecondItem "), I want
      > > the value of the attribute.[/color]
      >
      > Adding descriptions to enumeration constants
      > <URL:http://dotnet.mvps.org/dotnet/faqs/?id=enumdescrip tion&lang=en>
      >[/color]

      Thanks Herfried, I must have tried every combination of GetFieldInfo,
      GetMemberInfo, GetCustomAttrib utes, etc but not that one I guess.

      Comment

      Working...