473,399 Members | 4,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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
Nov 21 '05 #1
2 3378
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
10
by: James Brown | last post by:
I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING }; (it goes on and on like that) This is what I would like to do: TOKEN t1 = TOK_ID; // ok...
4
by: Nikhil Patel | last post by:
Hi all, I am a VB6 programmer and learning C#. I am currently reading a chapter on types. I have question regarding enums. Why do we need to convert enum members to the value that they represent?...
5
by: Andrea Williams | last post by:
I'm working with C# and I'm setting up some ENUM's I have a data and Business layer. I'm declaring a common enum for the Data Layer. The UI layer references the Bus layer and the bus layer...
6
by: Michael Isaacs | last post by:
Regarding use of enum's, I am wondering what the cost of memory is when creating the enumeration on the calling side, and then using it on the function/method side. See example below. If I...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
3
by: K. Wilder | last post by:
I need to declare a project level Enum that any procedure in any class can reference so there's continuity with the values. How do I do this? At present, if I declare a Module in a project and...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
4
by: ice8595 | last post by:
Hi there, I'm fairly new at this, and I am having a bit of trouble wrapping my head around some concepts of enum for a project. So any help would be greatly appreciated. Essentially I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.