Enum members same value problems


D

David

Hi,

namespace EnumTest
{
class Class1
{
public enum num
{
one = 1,
uno = 1,
eins = 1
}

[STAThread]
static void Main(string[] args)
{
Console.WriteLine( num.eins );
Console.WriteLine( num.uno );
Console.WriteLine( num.one );
}
}
}

outputs:
uno
uno
uno

How can I get the following output:
eins
uno
one

Origin Problem: I have a large Enum list with some double
values in it and need access to its diffrent names.

Anybody know, how to do this?

thanks

David
 
Ad

Advertisements

G

Guest

Hi David,

This code should resolve your problem:

foreach(string enumItemName in Enum.GetNames(typeof(num)) ) {
Console.WriteLine(enumItemName);
}

Cheers

Marcin
 
D

David

Hi,

How can I obtain the name of a specific double existing
enum value, like

public enum num
{
one = 1,
uno = 1,
eins = 1
}


string text = Enum.GetName( typeof( num ), num.one );

gets:

"one"

instead of

"uno"

Any ideas?

David
 
J

Jon Skeet [C# MVP]

David said:
namespace EnumTest
{
class Class1
{
public enum num
{
one = 1,
uno = 1,
eins = 1
}

[STAThread]
static void Main(string[] args)
{
Console.WriteLine( num.eins );
Console.WriteLine( num.uno );
Console.WriteLine( num.one );
}
}
}

outputs:
uno
uno
uno

How can I get the following output:
eins
uno
one

You can't. They're the same value. It's like saying that you want
Console.WriteLine (1+1) to display a different result from
Console.WriteLine (2)
 
Ad

Advertisements

G

Guest

David said:
Hi,

How can I obtain the name of a specific double existing
enum value, like

public enum num
{
one = 1,
uno = 1,
eins = 1
}


string text = Enum.GetName( typeof( num ), num.one );

gets:

"one"

instead of

"uno"

Any ideas?

There is a chance to get all enum names which values
are equal to "wanted" value:

Type enumType=typeof(enum);

foreach(string enumItemName in Enum.GetNames(enumType) ) {
if( (enum) Enum.Parse(enumType, enumItemName)==wantedEnumValue ) {
Console.WriteLine(enumItemName);
}
}

Cheers

Marcin
 

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