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