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

Loop an Enumeration

Hello,

I have an enumeration as follows:

public enum RoleType {
Administrator,
Collaborator,
...
}

How can I loop through each item in the enumeration and use it's
value? I was trying the following:

foreach (RoleType role in RoleType) {
Roles.CreateRole(role.ToString());
}

This is not working.

Thanks,
Miguel
Oct 9 '08 #1
4 1741
On Oct 9, 1:15*pm, shapper <mdmo...@gmail.comwrote:
Hello,

I have an enumeration as follows:

* public enum RoleType {
* * Administrator,
* * Collaborator,
* * ...
* }

How can I loop through each item in the enumeration and use it's
value? I was trying the following:

* * * foreach (RoleType role in RoleType) {
* * * * Roles.CreateRole(role.ToString());
* * * }

This is not working.

Thanks,
Miguel
I think I solved it:

foreach (string role in Enum.GetNames(typeof(RoleType))) {
Roles.CreateRole(role);
}

Not sure if it is the best way but ...

Thanks,
Miguel
Oct 9 '08 #2
On Oct 9, 1:33*pm, shapper <mdmo...@gmail.comwrote:
I think I solved it:

* * * foreach (string role in Enum.GetNames(typeof(RoleType))) {
* * * * Roles.CreateRole(role);
* * * }

Not sure if it is the best way but ...
That gets names - to get values, call Enum.GetValues(...)

Jon
Oct 9 '08 #3
On Oct 9, 8:15*am, shapper <mdmo...@gmail.comwrote:
Hello,

I have an enumeration as follows:

* public enum RoleType {
* * Administrator,
* * Collaborator,
* * ...
* }

How can I loop through each item in the enumeration and use it's
value? I was trying the following:

* * * foreach (RoleType role in RoleType) {
* * * * Roles.CreateRole(role.ToString());
* * * }

This is not working.
foreach (string roleType in Enum.GetNames(typeof(RoleType))
Oct 9 '08 #4
On Oct 9, 5:37*am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Oct 9, 1:33*pm, shapper <mdmo...@gmail.comwrote:
I think I solved it:
* * * foreach (string role in Enum.GetNames(typeof(RoleType))) {
* * * * Roles.CreateRole(role);
* * * }
Not sure if it is the best way but ...

That gets names - to get values, call Enum.GetValues(...)

Jon
like in

foreach (RoleType type in Enum.GetValues(typeof(RoleType)))
{
// your code
}

-Cnu
Oct 9 '08 #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"...
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 ...
5
by: David C | last post by:
This is very strange. Say I have code like this. I am simply looping through a collection object in a foreach loop. Course course = new Course(); foreach(Student s in course.Students) {...
25
by: David C | last post by:
I posted this question, and from the replies, I get the impression that I worded my posting very poorly, so let me try this again. While debugging and stepping through this foreach loop ...
1
by: Tim | last post by:
Hi, I'm very new to .NET and am programming in C#. I have a web application where i have two list boxes. Its kind of like a shopping card where you can add items from one 'locations' list box to...
9
by: Fred Zwarts | last post by:
What is the recommended way to loop over all enum values of a certain enum type? Consider the following definition: typdef enum {A=2, B, C=5, D} E; then for (E x = A; x <= D; ++x) { ... }
3
by: Davidoff | last post by:
Hi, I parse an XML file with a XSD schema. One XmlNode has an attribute whose type is a restriction of xs:string : <xs:simpleType name="stypeDay"> <xs:restriction base="xs:string">...
0
by: news.emn.fr | last post by:
Hello, i got this attribute <xs:attribute name="jour"> <xs:simpleType> <xs:restriction base="stypeJour"> </xs:restriction> </xs:simpleType> </xs:attribute>
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.