Enum Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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,
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(GetType(myData), anInteger),
myData)

aMyData = Nothing
aMyData = CType(anInteger, myData)

aString = aMyData.ToString()

aString = Nothing
aString = [Enum].GetName(GetType(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
 
It helps a lot...thanks.

Jay B. Harlow said:
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(GetType(myData), anInteger),
myData)

aMyData = Nothing
aMyData = CType(anInteger, myData)

aString = aMyData.ToString()

aString = Nothing
aString = [Enum].GetName(GetType(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 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top