472,372 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Enum

Hello,

I have an enum:

Public Enum Color
Red
Blue
Green
End Enum

I need:

1. Loop trough all enum items and get each one value (Ex: Red) and
index (Ex: 1).

For Each color As Color In [Enum].GetValues(GetType(Color))
???
Next

2. Get an enum value (Ex: Red) by providing its index (Ex: 1)

I have been trying to find some info about this but no success.

Thank you for your Help,

Miguel

Jan 12 '07 #1
3 5127

"shapper" <md*****@gmail.comwrote in message
news:11**********************@11g2000cwr.googlegro ups.com...
: Hello,
:
: I have an enum:
:
: Public Enum Color
: Red
: Blue
: Green
: End Enum
:
: I need:
:
: 1. Loop trough all enum items and get each one value (Ex: Red)
: and index (Ex: 1).
:
: For Each color As Color In [Enum].GetValues(GetType(Color))
: ???
: Next
:
: 2. Get an enum value (Ex: Red) by providing its index (Ex: 1)
:
: I have been trying to find some info about this but no success.
:
: Thank you for your Help,
:
: Miguel

The following should do what you want. Bear in mind that Enum's are
"value" types. In the code below, the System.Enum type's .Parse and
..GetNames functions perform "boxing" actions which are not
particularly efficient. This code example below is deliberately
designed to illustrate where this boxing and unboxing is occurring.

'---------------------------------------
'VB.NET 2.0
'---------------------------------------
Option Strict On
Imports System

Public Module [module]
Public Enum Color
Red
Blue
Green
End Enum

Public Sub Main

'get a list of names for this enumeration
For Each name As String In [Enum].GetNames(GetType(Color))

'convert the enumeration name into its value equivalent.
Dim obj As Object = [Enum].Parse(GetType(Color), name)
Dim value As Integer = CInt(obj)

'display the name and its value
Console.WriteLine(name & " = " & value)
Next

'get a list of the possible values for the enumeration
For Each value As Integer In [Enum].GetValues(GetType(Color))

'convert the value into its name equivalent
Dim obj As Object = CObj(value)
Dim name As String = [Enum].GetName(GetType(Color), obj)

'display the value and its name
Console.WriteLine(value & " = " & name)
Next

End sub

End Module
'---------------------------------------

Ralf
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.
Jan 13 '07 #2

_AnonCoward wrote:
"shapper" <md*****@gmail.comwrote in message
news:11**********************@11g2000cwr.googlegro ups.com...
: Hello,
:
: I have an enum:
:
: Public Enum Color
: Red
: Blue
: Green
: End Enum
:
: I need:
:
: 1. Loop trough all enum items and get each one value (Ex: Red)
: and index (Ex: 1).
:
: For Each color As Color In [Enum].GetValues(GetType(Color))
: ???
: Next
:
: 2. Get an enum value (Ex: Red) by providing its index (Ex: 1)
:
: I have been trying to find some info about this but no success.
:
: Thank you for your Help,
:
: Miguel

The following should do what you want. Bear in mind that Enum's are
"value" types. In the code below, the System.Enum type's .Parse and
.GetNames functions perform "boxing" actions which are not
particularly efficient. This code example below is deliberately
designed to illustrate where this boxing and unboxing is occurring.

'---------------------------------------
'VB.NET 2.0
'---------------------------------------
Option Strict On
Imports System

Public Module [module]
Public Enum Color
Red
Blue
Green
End Enum

Public Sub Main

'get a list of names for this enumeration
For Each name As String In [Enum].GetNames(GetType(Color))

'convert the enumeration name into its value equivalent.
Dim obj As Object = [Enum].Parse(GetType(Color), name)
Dim value As Integer = CInt(obj)

'display the name and its value
Console.WriteLine(name & " = " & value)
Next

'get a list of the possible values for the enumeration
For Each value As Integer In [Enum].GetValues(GetType(Color))

'convert the value into its name equivalent
Dim obj As Object = CObj(value)
Dim name As String = [Enum].GetName(GetType(Color), obj)

'display the value and its name
Console.WriteLine(value & " = " & name)
Next

End sub

End Module
'---------------------------------------

Ralf
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.
Hello,

After much reading and much googling I came up with this:
Get names and indexes of enum:
For Each color As Color In [Enum].GetValues(GetType(Color))

Dim name As String = MyF(color, Thread.CurrentThread.CurrentCulture)
Dim index As Integer = CType(color, Integer)

Response.Write("Name: " & name & " | Index: " & index.ToString & "<br
/>")

Next

MyF is a function which returns a string for a giving culture and enum
name:

Public Function MyF(ByVal color As Color, ByVal culture As CultureInfo)
As String
....
Get one enum name from its index:
Dim s As String = "20"
Dim i As Integer = CType(s, Integer)

If Color.IsDefined(GetType(Color), i) Then

Dim name As Color = CType(i, Color)
Response.Write(name.ToString)

End If

Need second opinion. What do you think?

I am asking this because my Response.Write seems to show the right
results but further on in my code I am having some problems and I am
not sure if it comes from here and until now I wasn't able to figure
this out.

Thanks,

Miguel

Jan 13 '07 #3

"shapper" <md*****@gmail.comwrote in message
news:11*********************@51g2000cwl.googlegrou ps.com...
:
: _AnonCoward wrote:
: >
: "shapper" <md*****@gmail.comwrote in message
: news:11**********************@11g2000cwr.googlegro ups.com...
: :
: : Hello,
: :
: : I have an enum:
: :
: : Public Enum Color
: : Red
: : Blue
: : Green
: : End Enum
: :
: : I need:
: :
: : 1. Loop trough all enum items and get each one value (Ex: Red)
: : and index (Ex: 1).
: :
: : For Each color As Color In [Enum].GetValues(GetType(Color))
: : ???
: : Next
: :
: : 2. Get an enum value (Ex: Red) by providing its index (Ex: 1)
: :
: : I have been trying to find some info about this but no success.
: :
: : Thank you for your Help,
: :
: : Miguel
: >
: The following should do what you want. Bear in mind that Enum's
: are "value" types. In the code below, the System.Enum type's
: .Parse and .GetNames functions perform "boxing" actions which
: are not particularly efficient. This code example below is
: deliberately designed to illustrate where this boxing and
: unboxing is occurring.
: >
: '---------------------------------------
: 'VB.NET 2.0
: '---------------------------------------
: Option Strict On
: Imports System
: >
: Public Module [module]
: Public Enum Color
: Red
: Blue
: Green
: End Enum
: >
: Public Sub Main
: >
: 'get a list of names for this enumeration
: For Each name As String In [Enum].GetNames(GetType(Color))
: >
: 'convert the enumeration name into its value equivalent.
: Dim obj As Object = [Enum].Parse(GetType(Color), name)
: Dim value As Integer = CInt(obj)
: >
: 'display the name and its value
: Console.WriteLine(name & " = " & value)
: Next
: >
: 'get a list of the possible values for the enumeration
: For Each value As Integer In [Enum].GetValues(GetType(Color))
: >
: 'convert the value into its name equivalent
: Dim obj As Object = CObj(value)
: Dim name As String = [Enum].GetName(GetType(Color), obj)
: >
: 'display the value and its name
: Console.WriteLine(value & " = " & name)
: Next
: >
: End sub
: >
: End Module
: '---------------------------------------
: >
: Ralf
: --
: --
: ----------------------------------------------------------
: * ^~^ ^~^ *
: * _ {~ ~} {~ ~} _ *
: * /_``>*< >*<''_\ *
: * (\--_)++) (++(_--/) *
: ----------------------------------------------------------
: There are no advanced students in Aikido - there are only
: competent beginners. There are no advanced techniques -
: only the correct application of basic principles.
:
: Hello,
:
: After much reading and much googling I came up with this:
:
: Get names and indexes of enum:
:
: For Each color As Color In [Enum].GetValues(GetType(Color))
:
: Dim name As String = _
: MyF(color, Thread.CurrentThread.CurrentCulture)
:
: Dim index As Integer = CType(color, Integer)
:
: Response.Write("Name: " & name & " | Index: " & _
: index.ToString & "<br />")
:
: Next
:
: MyF is a function which returns a string for a giving culture and
: enum name:
:
: Public Function MyF(ByVal color As Color, _
: ByVal culture As CultureInfo) As String
: ...
:
: Get one enum name from its index:
:
: Dim s As String = "20"
: Dim i As Integer = CType(s, Integer)
:
: If Color.IsDefined(GetType(Color), i) Then
:
: Dim name As Color = CType(i, Color)
: Response.Write(name.ToString)
:
: End If
:
: Need second opinion. What do you think?
:
: I am asking this because my Response.Write seems to show the right
: results but further on in my code I am having some problems and I am
: not sure if it comes from here and until now I wasn't able to figure
: this out.
:
: Thanks,

What problems are you having, specifically? Also, you haven't provided
the dteails of the MyF Function, so it's impossible to say from this
what affect it's having later in you code.

Ralf
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.
Jan 13 '07 #4

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...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
2
by: Dennis | last post by:
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...
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)
10
by: Randy | last post by:
Hi, Can anyone point me to a complete, compilable example of Besser's ENUM++ mechanism? I downloaded it from CUJ and gave it a try but got errors just trying to compile the header enum.h. ...
1
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
2
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded 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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.