By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,890 Members | 1,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,890 IT Pros & Developers. It's quick & easy.

Enum Question

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <De****@discussions.microsoft.com> wrote in message
news:B2**********************************@microsof t.com...
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

Nov 21 '05 #2

P: n/a
It helps a lot...thanks.

"Jay B. Harlow [MVP - Outlook]" wrote:
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" <De****@discussions.microsoft.com> wrote in message
news:B2**********************************@microsof t.com...
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


Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.