473,683 Members | 3,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(GetT ype(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 5229

"shapper" <md*****@gmail. comwrote in message
news:11******** **************@ 11g2000cwr.goog legroups.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(GetT ype(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(GetTy pe(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.WriteLi ne(name & " = " & value)
Next

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

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

'display the value and its name
Console.WriteLi ne(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.goog legroups.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(GetT ype(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(GetTy pe(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.WriteLi ne(name & " = " & value)
Next

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

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

'display the value and its name
Console.WriteLi ne(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(GetT ype(Color))

Dim name As String = MyF(color, Thread.CurrentT hread.CurrentCu lture)
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******** *************@5 1g2000cwl.googl egroups.com...
:
: _AnonCoward wrote:
: >
: "shapper" <md*****@gmail. comwrote in message
: news:11******** **************@ 11g2000cwr.goog legroups.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(GetT ype(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(GetTy pe(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.WriteLi ne(name & " = " & value)
: Next
: >
: 'get a list of the possible values for the enumeration
: For Each value As Integer In [Enum].GetValues(GetT ype(Color))
: >
: 'convert the value into its name equivalent
: Dim obj As Object = CObj(value)
: Dim name As String = [Enum].GetName(GetTyp e(Color), obj)
: >
: 'display the value and its name
: Console.WriteLi ne(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(GetT ype(Color))
:
: Dim name As String = _
: MyF(color, Thread.CurrentT hread.CurrentCu lture)
:
: 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
4840
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 EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
21
4592
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
3594
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(); Control c = f; An enum value inherits from int but it doesn't get implicitly converted: HorizontalAlignment h = HorizontalAlignment.Center;
18
11347
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
3395
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 and return the name "FirstData"
13
12381
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
3893
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. --Randy Yates
1
2456
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 installed the boost library. This is using gcc under FC3.
2
2118
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 installed the boost library. This is using gcc under FC3.
34
11184
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 snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
0
8568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9043
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8922
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7582
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6429
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5789
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4519
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.