Loop an Enumeration

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

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
 
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
 
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
 
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))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Pass empty string 4
Sring[] to List<MyEnumeration> 5
Enumeration 2
Looping through multiple enumerations 1
Enumeration 4
Enumerator 2
List. Check items 3
Enumeration and Class 4

Back
Top