1/06/2010

Enumerate an enumeration!

 

A quick way to find all of the values of an enumeration (that Microsoft may have not completely documented).

System.Enum.GetNames returns an array of strings with the names

System.Enum.Parse looks up the numeric value using the string name

 

This example lists the value of a SharePoint enumeration named SpBasePermissions:

 

C# example:

//show name, decimal and hex
foreach (string enumName in Enum.GetNames(typeof(SPBasePermissions)))
{
    Console.WriteLine(String.Format("Item: {0,-25} Value: {1,20} {1,20:X}",
        enumName, (ulong)Enum.Parse(typeof(SPBasePermissions), enumName)));
}

 

VB.Net example:

For Each enumName As String In [Enum].GetNames(GetType(SPBasePermissions))
    Console.WriteLine(String.Format("Item: {0,-23} Value: {1,20:D} {1,20:X}", _
        enumName, [Enum].Parse(GetType(SPBasePermissions), enumName)))
Next

 

Result:

image

No comments:

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.