enum basics

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
 
G

gerry

MyEnum.test1.ToString( );

or

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

or

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

Jeroen

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...
 
R

rodchar

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
 
A

Alun Harford

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
 
B

Ben Voigt [C++ MVP]

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();
 
B

Ben Voigt [C++ MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top