Geeks With Blogs
Moez Mousavi

I love enums, they are organising my head, sometime you might need to traverse through all enum members for any reason. Say you would like to dynamically add controls to the form on the base of enum...

foreach (MyTypeEnum myType in Enum.GetValues(typeof(MyTypeEnum))
{
          //myType.ToString()        
}

Or say put all enum values in a list box

 var lst = Enum.GetValues(typeof(MyTypeEnum)).OfType<MyTypeEnum>()
                       .Select(o => new ListItem { Text = o.ToString(),

Value = ((int)o).ToString() });
 
           

myList.Items.AddRange(lst.ToArray());
 

How cool is that?

 

sometime, you might want to convert your string value back to enum, or actually you want to do parsing:

MyTypeEnum role =   (MyTypeEnum) Enum.Parse(typeof(MyTypeEnum), roleString);

 

Posted on Friday, April 22, 2011 6:03 PM C# , Linq | Back to top

Copyright © MoezMousavi | Powered by: GeeksWithBlogs.net