473,395 Members | 1,766 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,395 software developers and data experts.

iterate Enum

how to iterate through the members of an Enum ?

Enum EnumAction
None = 0
Action1 = 1
Action2 = 2
Action3 = 3
Action4 = 4
End Enum

Dim vAction As EnumAction

For Each vAction In Enum1 ....

Next vAction

triggers error " 'EnumAction' is a type and cannot be used in an expression
"
Nov 21 '05 #1
4 3164
you can do something like this:

Enum testenum
one = 1
two
three
four
five
End Enum

Dim arr As Array = [Enum].GetValues(GetType(testenum))
For i As Integer = arr.GetLowerBound(0) To arr.GetUpperBound(0)
Console.WriteLine("Name: {0}" & vbTab & "Value: {1}", _
[Enum].GetName(GetType(testenum), arr.GetValue(i)), _
CInt(arr.GetValue(i)))
Next

hope that helps..
Imran.

"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:em**************@TK2MSFTNGP11.phx.gbl...
how to iterate through the members of an Enum ?

Enum EnumAction
None = 0
Action1 = 1
Action2 = 2
Action3 = 3
Action4 = 4
End Enum

Dim vAction As EnumAction

For Each vAction In Enum1 ....

Next vAction

triggers error " 'EnumAction' is a type and cannot be used in an
expression
"

Nov 21 '05 #2
John,
In addition to Imran's sample, I normally use:

For Each vAction In [Enum].GetValues(GetType(EnumAction))
Debug.WriteLine(vAction)
Next vAction

In addition to System.Enum.GetValues which gets the values of an Enum, there
is Format, GetNames, IsDefined, Parse and a number of overloaded ToObject
shared methods each which are equally useful for the job they perform.

Hope this helps
Jay

"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:em**************@TK2MSFTNGP11.phx.gbl...
how to iterate through the members of an Enum ?

Enum EnumAction
None = 0
Action1 = 1
Action2 = 2
Action3 = 3
Action4 = 4
End Enum

Dim vAction As EnumAction

For Each vAction In Enum1 ....

Next vAction

triggers error " 'EnumAction' is a type and cannot be used in an
expression
"

Nov 21 '05 #3
Am I missing something? I don't see any instantiation so it seems that the
message stating that it is a type is correct and appropriate.
Serious quest, by the way.
BobJ
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:em**************@TK2MSFTNGP11.phx.gbl...
how to iterate through the members of an Enum ?

Enum EnumAction
None = 0
Action1 = 1
Action2 = 2
Action3 = 3
Action4 = 4
End Enum

Dim vAction As EnumAction

For Each vAction In Enum1 ....

Next vAction

triggers error " 'EnumAction' is a type and cannot be used in an expression "

Nov 21 '05 #4
Am I missing something? I don't see any instantiation so it seems that the
message stating that it is a type is correct and appropriate.
Serious quest, by the way.
BobJ
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:em**************@TK2MSFTNGP11.phx.gbl...
how to iterate through the members of an Enum ?

Enum EnumAction
None = 0
Action1 = 1
Action2 = 2
Action3 = 3
Action4 = 4
End Enum

Dim vAction As EnumAction

For Each vAction In Enum1 ....

Next vAction

triggers error " 'EnumAction' is a type and cannot be used in an expression "

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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 {
3
by: Billy Porter | last post by:
Greetings, If I populate an ArrayList with values from, let's say, the Keys enum: ArrayList a = new ArrayList(); a.Add(Keys.Home); a.Add(Keys.End); ....then how do I iterate through the...
2
by: Mark | last post by:
Assume you've declared an enum .... public enum MyEnum { First, Second, Third } Without knowing the values of the enum, is it possible to iterate through all the possible values...
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();...
2
by: John A Grandy | last post by:
how to iterate through the members of an Enum ? Enum EnumAction None = 0 Action1 = 1 Action2 = 2 Action3 = 3 Action4 = 4 End Enum
2
by: Lee Gillie | last post by:
I see this was posted a long time ago, but it has since expired. Given.... Public Enum Fruit Apple Banana Cherry Peach Strawberry
6
by: Steven Woody | last post by:
is there any way in C leting me iterate all integer constants in a enum type ? their values might not be continue. thanks. - woody
2
by: Rico | last post by:
Hello, Is there any way to loop through an enumerator? I've searched google, but just about every link I find shows the VB.net solution, even when I use "MS+Access" in the search string. ...
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: garth | last post by:
Hi, Is it possible to iterate over and enumerated type in C#? If so please can you paste a simple exampel up that covers the whole process. Thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.