tags:

views:

144

answers:

3

Hi,

I have an enum:

public enum baseKey : uint
{  
    HKEY_CLASSES_ROOT = 0x80000000,
    HKEY_CURRENT_USER = 0x80000001,
    HKEY_LOCAL_MACHINE = 0x80000002,
    HKEY_USERS = 0x80000003,
    HKEY_CURRENT_CONFIG = 0x80000005
}

How can i, given the string "HKEY_LOCAL_MACHINE", get a value 0x80000002 based on the enum ?

Thanks Luiz Costa

+14  A: 
(uint)Enum.Parse(typeof(baseKey), "HKEY_LOCAL_MACHINE")
Mehrdad Afshari
+5  A: 
var value = (uint) Enum.Parse(typeof(baseKey), someString);
Joseph
+8  A: 

With some error handling...

uint key = 0;
string s = "HKEY_LOCAL_MACHINE";
try
{
   key = (uint)Enum.Parse(typeof(baseKey), s);
}
catch(ArgumentException)
{
   //unknown string or s is null
}
Frank Bollack