I think this question needs some code:
private TypeValues GetEnumValues(Type enumType, string description)
{
TypeValues wtv = new TypeValues();
wtv.TypeValueDescription = description;
List<string> values = Enum.GetNames(enumType).ToList();
foreach (string v in values)
{
//how to get the integer value of the enum value 'v' ?????
wtv.TypeValues.Add(new TypeValue() { Code = v, Description = v });
}
return wtv;
}
Call it like this:
GetEnumValues(typeof(AanhefType), "some name");
In the GetEnumValues
function i have the values of the enum. So i iterate the values and i want to get the integer value of that enum value also.
So my values are 'red' and 'green', and i also want to get 0 and 1.
When i have the Enum in my function, i can create the enum value from the string and cast it to that enum and then cast it to an int, but in this case, i don't have the enum itself, but only the type of the enum.
I tried passing in the actual enum as a parameter too, but i'm not allowed to pass an enum as a parameter.
So now i'm stuck.....
private TypeValues GetEnumValues(Type enumType, string description)
{
TypeValues wtv = new TypeValues();
wtv.TypeValueDescription = description;
List<string> values = Enum.GetNames(enumType).ToList();
foreach (string v in values)
{
//how to get the integer value of the enum value 'v' ?????
int value = (int)Enum.Parse(enumType, v);
wtv.TypeValues.Add(new TypeValue() { Code = v, Description = v });
}
return wtv;
}
http://msdn.microsoft.com/en-us/library/essfb559.aspx
Enum.Parse will take a Type and a String and return a reference to one of the enum values - which can then simply be cast to int.
Try
(int)Enum.Parse(enumType, v)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With