473,406 Members | 2,369 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,406 software developers and data experts.

Enumeration. Get values

Hello,

Is there a way to loop trough all enumeration values?

Thanks,

Miguel

Jan 9 '07 #1
4 1381
Hi,

shapper wrote:
Hello,

Is there a way to loop trough all enumeration values?

Thanks,

Miguel
Yes:

enum MyEnum
{
// ...
}

Then:

MyEnum[] allEnumValues = (MyEnum[]) Enum.GetValues( typeof( MyEnum ) );

Then you can loop using foreach.

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Jan 9 '07 #2
Hi,

I used the following code:

Public Enum Roles

End Enum

' Loop through each roles enumeration name
For Each role As String In Roles.GetNames(GetType(Roles))
MyFunction(role)
Next

This is working if MyFunction is as follows:
Function MyFunction(ByVal role As String)

However, I would like to use something like:
Function MyFunction(ByVal role As Roles)

How can I make this work?

Thanks,
Miguel
Laurent Bugnion [MVP] wrote:
Hi,

shapper wrote:
Hello,

Is there a way to loop trough all enumeration values?

Thanks,

Miguel

Yes:

enum MyEnum
{
// ...
}

Then:

MyEnum[] allEnumValues = (MyEnum[]) Enum.GetValues( typeof( MyEnum ) );

Then you can loop using foreach.

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Jan 9 '07 #3
Hi,

shapper wrote:
Hi,

I used the following code:

Public Enum Roles

End Enum

' Loop through each roles enumeration name
For Each role As String In Roles.GetNames(GetType(Roles))
MyFunction(role)
Next

This is working if MyFunction is as follows:
Function MyFunction(ByVal role As String)

However, I would like to use something like:
Function MyFunction(ByVal role As Roles)

How can I make this work?

Thanks,
Miguel
Using GetNames returns the names as string. Using GetValues returns the
values as enumeration, so this is what you want.

I described it to you in my prior post quoted below.

HTH,
Laurent

>enum MyEnum
{
// ...
}

Then:

MyEnum[] allEnumValues = (MyEnum[]) Enum.GetValues( typeof( MyEnum ) );

Then you can loop using foreach.

--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Jan 9 '07 #4
Hi,

shapper wrote:
I used the following code:

Public Enum Roles

End Enum

' Loop through each roles enumeration name
For Each role As String In Roles.GetNames(GetType(Roles))
MyFunction(role)
Next

This is working if MyFunction is as follows:
Function MyFunction(ByVal role As String)

However, I would like to use something like:
Function MyFunction(ByVal role As Roles)
to me it seems that you simply misunderstood something.
If you would like to have an argument in your function that is geared at an
enum, you should actually be fine with the way you did above. All you need
in order to use the enum in your function is a) from the caller, you need
to call it as in ClassName.EnumName.EnumValue and b) the function utilizing
the value must be within the enum's scope or the enum must also include the
complete path ([namespace], class-name, etc.).
Consider this:

public class MyClass
public enum MyEnum
MyValue1=1
MyValue2=2
end enum

public sub MyFunction(byval x as MyEnum) as boolean
select case x
case MyEnum.MyValue1
'...
case MyEnum.MyValue2
'...
case else
'...
end select
return ...
end sub
end class

A call to the above function from outside of MyClass could look like this:
dim cM as MyClass
dim XY as boolean=cm.MyFunction(MyClass.MyEnum.MyValue2)

Hence, is it possible that you took it the wrong way and there isn't any
need for GetNames ..? At least I don't see what you would need it for in
your scenario.

Cheers,
Olaf
--
My .02: www.Resources.IntuiDev.com
Jan 9 '07 #5

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

Similar topics

1
by: Justin Wright | last post by:
I know that I can set up an enumeration as follows ( just typed in quick so may have syntax errors ): <xsd:simpleType name="colors"> <xsd:restriction base="xsd:string"> <xsd:enumeration...
8
by: aevans1108 | last post by:
Greetings I can't seem to inherit enumerated values from a globally defined type in my XML schema. XmlSchema.Compile() doesn't like it. Here's the schema. <?xml version="1.0"...
1
by: Sergey Poberezovskiy | last post by:
Hi, I have a simple enumeration in my schema: <xs:element name="el_1"> <xs:simpleType> <xs:restiction base="xs:string"> <xs:enumeration value="value and space 1"/> <xs:enumeration...
1
by: Jamie Winder via .NET 247 | last post by:
Is it possible to iterate through all of the possible values of an enumeration? (with foreach, maybe?) What I need to do is fill a ComboBox with all possible values for an enumeration. e.g ...
2
by: Mark | last post by:
Assume you have an enumeration like PhoneType { Home, Business, Cell }. This enumeration corresponds with a lookup/dictionary table in your database like: phone_cd | phone_descr 1 ...
3
by: Sampson | last post by:
I have a question about enumeration and how to populate them during runtime. I am using vb.net but will happily take any advice in c# as well. Here is an example to help illustrate what I am...
5
by: Dave | last post by:
How would I go about filling a combo box with some existing enumeration values? Can I bind it to the enumeration? Then, once the selection is made from the combo box, set a property to the...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
2
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
how do I code generic functions to return the next item in an enumeration a) sorted by name, b) sorted by value c) sorted by declaration in a round-robin style ? for example the enum is Enum...
0
by: puvit82 | last post by:
My problem is as follows, any advice / suggestion would be greatly appreciated: Lets suppose that I have defined a simpleType "addressType" with 3 enumeration values (Home, Office, Vacation)...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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,...
0
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...

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.