Main Site Documentation

Get enum value name


#1

Say you have an enum.


public enum MyEnum
{
Value1,
Value2,
Value3
}

Now I want to get the value name, and not the value index. I was used to do it like this:


MyEnum myenum = MyEnum.Value3;
myenum.ToString();

But this returns 2 instead of Value3. How can I get the value name in .NET Micro Framework?


#2

Wouter, look here: http://www.tinyclr.com/forum/1/1395/


#3

I sometimes go the quick and easy route with:

public static string GetMyEnumName(MyEnum myEnum)
{
    Debug.Print("MyEnum Name:" + myEnum.ToString());
    switch (myEnum)
    {
        case MyEnum.V1:
            return "V1";
        case MyEnum.V2:
            return "V2";
        case MyEnum.V3:
            return "V3";
        default:
            return "Unknown";
    }
}

public enum MyEnum
{
    V1,
    V2,
    V3
}

#4

I’ll first try the reflection method and see if it works. If it doesn’t I’ll use William’s method.

Thanks


#5

Reflection doesn’t seem to work. The only field returned is “value__”.

Going for the switch-case solution :slight_smile: