get name of enum element programmatically?

  • Thread starter Thread starter Zytan
  • Start date Start date
Z

Zytan

I have an enum with a bunch of names / values. This is nice as I can
code using the names, instead of memorizing the values.

But, let's say something gives me a value. Now, while my code is happy
using a number, what if I want to display the name that this value
means?

Can I get the enum name, as it is typed in code, from the enum? or do
I need to write another array of strings to accomplish this?

Zytan
 
Zytan said:
I have an enum with a bunch of names / values. This is nice as I can
code using the names, instead of memorizing the values.

But, let's say something gives me a value. Now, while my code is happy
using a number, what if I want to display the name that this value
means?

Can I get the enum name, as it is typed in code, from the enum? or do
I need to write another array of strings to accomplish this?

Call ToString() on it:

using System;

enum Poster
{
Zykan,
Jon
}

class Test
{
static void Main()
{
Poster p = Poster.Jon;
string name = p.ToString();

Console.WriteLine (name);
}
}
 
Zytan said:
I have an enum with a bunch of names / values. This is nice as I can
code using the names, instead of memorizing the values.

But, let's say something gives me a value. Now, while my code is happy
using a number, what if I want to display the name that this value
means?

Can I get the enum name, as it is typed in code, from the enum? or do
I need to write another array of strings to accomplish this?

Zytan

Just use the ToString method.
 
Jon and Goran, you guys answer everything. Thanks for your help, I
appreciate it. ToString, of course, I should have guessed.

Zytan
 
Back
Top