Enum Question

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

    Enum Question

    I have an enum as follows:

    Public Enum myData
    FirstData = 6
    SecondData = 7
    end enum

    Is there anyway that I can return the Enum names by their value, i.e., I
    want to input 6 into a function and return the name "FirstData"

    Thanks.
    --
    Dennis in Houston
  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Enum Question

    Dennis,
    Have you looked at the shared functions on System.Enum?

    You can use either Enum.ToObject or Ctype to convert an integer into the
    corresponding Enum value.

    You can use ToString or Enum.GetName to convert a corresponding Enum value
    into a String.

    You can use Enum.Parse to convert a string into the corresponding Enum
    Value.

    Dim anInteger As Integer = 6
    Dim aMyData As myData
    Dim aString As String

    aMyData = DirectCast([Enum].ToObject(GetTy pe(myData), anInteger),
    myData)

    aMyData = Nothing
    aMyData = CType(anInteger , myData)

    aString = aMyData.ToStrin g()

    aString = Nothing
    aString = [Enum].GetName(GetTyp e(myData), aMyData)

    aMyData = Nothing
    aMyData = DirectCast([Enum].Parse(GetType( myData), aString), myData)

    There are a number of other useful shared methods on System.Enum, such as
    Enum.GetValues to get the list of values defined on an Enum, Enum.GetNames
    to get the list of names, and Enum.IsDefined to verify the enum value is one
    of the defined values (taking the Flags attribute into effect).

    Note, [Enum] says to treat Enum as an identifier (as in System.Enum) instead
    of as a keyword (as in "Public Enum myData").

    Hope this helps
    Jay

    "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
    news:B204C516-61AE-45CA-9322-993C754E1805@mi crosoft.com...[color=blue]
    >I have an enum as follows:
    >
    > Public Enum myData
    > FirstData = 6
    > SecondData = 7
    > end enum
    >
    > Is there anyway that I can return the Enum names by their value, i.e., I
    > want to input 6 into a function and return the name "FirstData"
    >
    > Thanks.
    > --
    > Dennis in Houston[/color]


    Comment

    • Dennis

      #3
      Re: Enum Question

      It helps a lot...thanks.

      "Jay B. Harlow [MVP - Outlook]" wrote:
      [color=blue]
      > Dennis,
      > Have you looked at the shared functions on System.Enum?
      >
      > You can use either Enum.ToObject or Ctype to convert an integer into the
      > corresponding Enum value.
      >
      > You can use ToString or Enum.GetName to convert a corresponding Enum value
      > into a String.
      >
      > You can use Enum.Parse to convert a string into the corresponding Enum
      > Value.
      >
      > Dim anInteger As Integer = 6
      > Dim aMyData As myData
      > Dim aString As String
      >
      > aMyData = DirectCast([Enum].ToObject(GetTy pe(myData), anInteger),
      > myData)
      >
      > aMyData = Nothing
      > aMyData = CType(anInteger , myData)
      >
      > aString = aMyData.ToStrin g()
      >
      > aString = Nothing
      > aString = [Enum].GetName(GetTyp e(myData), aMyData)
      >
      > aMyData = Nothing
      > aMyData = DirectCast([Enum].Parse(GetType( myData), aString), myData)
      >
      > There are a number of other useful shared methods on System.Enum, such as
      > Enum.GetValues to get the list of values defined on an Enum, Enum.GetNames
      > to get the list of names, and Enum.IsDefined to verify the enum value is one
      > of the defined values (taking the Flags attribute into effect).
      >
      > Note, [Enum] says to treat Enum as an identifier (as in System.Enum) instead
      > of as a keyword (as in "Public Enum myData").
      >
      > Hope this helps
      > Jay
      >
      > "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
      > news:B204C516-61AE-45CA-9322-993C754E1805@mi crosoft.com...[color=green]
      > >I have an enum as follows:
      > >
      > > Public Enum myData
      > > FirstData = 6
      > > SecondData = 7
      > > end enum
      > >
      > > Is there anyway that I can return the Enum names by their value, i.e., I
      > > want to input 6 into a function and return the name "FirstData"
      > >
      > > Thanks.
      > > --
      > > Dennis in Houston[/color]
      >
      >
      >[/color]

      Comment

      Working...