enum basics

  • Thread starter Thread starter rodchar
  • Start date Start date
R

rodchar

hey all,
is there anyway to ask the Enum what the string value is with a given value?
Like how you would access an element of an array?

enum Myenum
{
test1=1
test2=2
}

This won't work but may better explain:
String a = MyEnum[1];

thanks,
rodchar
 
MyEnum.test1.ToString( );

or

Enum.GetName ( typeof ( MyEnum ) , MyEnum.test1 )

or

Enum.GetNames ( typeof ( MyEnum ) ) [ 0 ]
 
What is "none of the above", Alex?

All options will cause you problems if you obfuscate your code, found
that one out the hard way ;-).

Regards,
Jeroen

PS. If this thread was meant as a particular question, then I missed
it...
 
Thanks Gerry, i appreciate it.
rod.

gerry said:
MyEnum.test1.ToString( );

or

Enum.GetName ( typeof ( MyEnum ) , MyEnum.test1 )

or

Enum.GetNames ( typeof ( MyEnum ) ) [ 0 ]



rodchar said:
hey all,
is there anyway to ask the Enum what the string value is with a given
value?
Like how you would access an element of an array?

enum Myenum
{
test1=1
test2=2
}

This won't work but may better explain:
String a = MyEnum[1];

thanks,
rodchar
 
Jeroen said:
What is "none of the above", Alex?

All options will cause you problems if you obfuscate your code, found
that one out the hard way ;-).

Hmm... then somewhere you needed to map those enums to strings, which
suggests the obfuscation was totally pointless anyway. As per usual.

Alun Harford
 
rodchar said:
hey all,
is there anyway to ask the Enum what the string value is with a given
value?
Like how you would access an element of an array?

enum Myenum
{
test1=1
test2=2
}

This won't work but may better explain:
String a = MyEnum[1];

int i = 1;
((Myenum)i).ToString();
 
gerry said:
MyEnum.test1.ToString( );

ok, but you specified the enum element by name already
or

Enum.GetName ( typeof ( MyEnum ) , MyEnum.test1 )

ok, but you specified the enum element by name again
or

Enum.GetNames ( typeof ( MyEnum ) ) [ 0 ]

This returns the first name, not the name corresponding to the value (0).
rodchar said:
hey all,
is there anyway to ask the Enum what the string value is with a given
value?
Like how you would access an element of an array?

enum Myenum
{
test1=1
test2=2
}

This won't work but may better explain:
String a = MyEnum[1];

thanks,
rodchar
 
Back
Top