Answers
Nov 24, 2006 - 12:17 AM
I tried this, but got an exception in the first line:
value is a string that contains the index
Array values = Enum.GetValues(typeof(MYENUM));
value = (string) values.GetValue(Convert.ToInt32(value));
value is a string that contains the index
Array values = Enum.GetValues(typeof(MYENUM));
value = (string) values.GetValue(Convert.ToInt32(value));
Nov 26, 2006 - 12:24 PM
Confusing!
This works for me:
enum MyEnum {One = 1, Two = 2, Three = 3, Four = 4};
static void Main(string[] args) {
string strIndex = "2";
string value;
Array values = Enum.GetValues(typeof(MyEnum));
value = values.GetValue(Convert.ToInt32(strIndex)).ToString();
Console.WriteLine(value);
Console.Read();
}
Cheers
Peter
This works for me:
enum MyEnum {One = 1, Two = 2, Three = 3, Four = 4};
static void Main(string[] args) {
string strIndex = "2";
string value;
Array values = Enum.GetValues(typeof(MyEnum));
value = values.GetValue(Convert.ToInt32(strIndex)).ToString();
Console.WriteLine(value);
Console.Read();
}
Cheers
Peter
Dec 15, 2006 - 08:20 AM
ah, i think i only assigned the names to the enum, without giving them a value.
I thought they automatically had an index from 0 and up...
Thanks for your help, Peter.
I thought they automatically had an index from 0 and up...
Thanks for your help, Peter.
Dec 22, 2006 - 03:30 AM
Thank you very very much for ur detailed example which i want.
Feb 19, 2012 - 10:04 AM
// Just use (int) in front of enum variable.
// Example -------------------------
enum MyEnum {One=1,Two=2}
private void echoEnumValue(MyEnum pParamEnum)
{
string msg = ((int)pParamEnum).ToString();
MessageBox.Show(msg);
}
// Example -------------------------
enum MyEnum {One=1,Two=2}
private void echoEnumValue(MyEnum pParamEnum)
{
string msg = ((int)pParamEnum).ToString();
MessageBox.Show(msg);
}
Add New Comment