Loop an Enumeration

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

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
 
J

Jon Skeet [C# MVP]

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
 
Z

zacks

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

Top