Enum members same value problems

  • Thread starter Thread starter David
  • Start date Start date
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
 
Hi David,

This code should resolve your problem:

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

Cheers

Marcin
 
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
 
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)
 
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
 
Back
Top