it blows up at this line when i enter in
00AA or 0x00AA, i guess its not really possible to put a enumeraton out
of order
string value =
commonFunctions.GetTestName((Dictionary.Common.ValidtTest)Enum.Parse(typeof(Dictionary.Common.ValidTest),
"00AA"));
public enum ValidTest
{
[Description("TEST_A")] CAT = 0x0000,
[Description("TEST_B")] DOG = 0x00AA,
[Description("TEST_C")] BEAR = 0x0002,
}
public string GetTestName(Enum eventValue)
{
ValidTest vtn = (ValidTest)eventValue;
// Get the type
Type type = eventValue.GetType();
// Get fieldinfo for this type
FieldInfo fieldInfo = type.GetField(vtn.ToString());
// Get the stringvalue attributes
DescriptionAttribute[] attribs = fieldInfo.GetCustomAttributes(
typeof(DescriptionAttribute), false) as
DescriptionAttribute[];
// Return the first if there was a match.
return attribs.Length > 0 ? attribs[0].Description : null;
}
Loading
David SmithPosted Oct 17, 2010, 8:44 PM
This works perfect
public class commonAnimals
{
static private Dictionary<string, string> ValidAnimalsNames= new Dictionary<string,string>();
static public void initDictionary()
{
ValidAnimalsNames.Add("0000", "CAT");
ValidAnimalsNames.Add("00AA", "DOG");
ValidAnimalsNames.Add("0002", "BEAR");
}
static public string GetValidAnimalsName(string key)
{
string value = string.Empty;
if (ValidAnimalsNames.ContainsKey(key))
{
value = ValidAnimalsNames[key];
}
return value;
}
}
Bryian TanPosted Oct 17, 2010, 1:20 AM
[Description("TEST_A")] CAT = 0x0000, //value is 0
[Description("TEST_B")] DOG = 0x00AA, //value is 170
[Description("TEST_C")] BEAR = 0x0002, //value is 2
Enum.Parse(typeof(Dictionary.Common.ValidTest), "00AA")
The above line is trying to look for the value "00AA" but not found.
Try it with "0170" and it should return "TEST_B"