Enumeration

S

shapper

Hello,

I have a list of a class, Theme, which has two properties:
IList<Level> Levels and String LevelsCsv.

Level is an enumeration ...
LevelsCsv is in CSV format where each item is an item of the
enumeration.

So basically I need to go through each theme and convert the LevelsCvs
format to IList<Level>

foreach (Theme t in AllThemes) {
t.Levels = t.LevelsCsv.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries) ??? )
}

My problem is more with the parsing the CSV elements to enumeration
values.

How can I do this?

Thanks,
Miguel
 
S

shapper

[...]
My problem is more with the parsing the CSV elements to enumeration
values.
How can I do this?

Impossible to say with such a vague question.  But you might find
Enum.Parse() useful.

Pete

Ok, let me give a practical example:

public enum Role {
Administrator = 1,
Collaborator,
Visitor
} // Role

public class User {
IList<Role> Roles { get; set; }
String RolesCsv { get; set; }
}

foreach (User u in AllUsers) {
u.Roles = u.RolesCsv.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries) ??? )
}

If, for example, a user has RolesCsv = "Administrator, Visitor" then
Roles property will get two items "Role.Administrator" and
"Role.Visitor".

Do you understand my code?

Thanks,
Miguel
 
S

shapper

[...]
My problem is more with the parsing the CSV elements to enumeration
values.
How can I do this?
Impossible to say with such a vague question.  But you might find
Enum.Parse() useful.

Ok, let me give a practical example:

  public enum Role {
    Administrator = 1,
    Collaborator,
    Visitor
  } // Role

  public class User {
    IList<Role> Roles { get; set; }
    String RolesCsv { get; set; }
  }

  foreach (User u in AllUsers) {
    u.Roles = u.RolesCsv.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries) ??? )

}

If, for example, a user has RolesCsv = "Administrator, Visitor" then
Roles property will get two items "Role.Administrator" and
"Role.Visitor".

Do you understand my code?

Thanks,
Miguel

Hello,

I was able to solve it using:

foreach (Theme t in vAccount.Account.Profile.Tutor.Themes) {
t.Levels = t.LevelsCsv.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(l => (Level)Enum.Parse
(typeof(Level), l)).ToList();
}

Thanks,
Miguel
 

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