Enum ToString()

I have a question. In full .NET, this code prints “Two”

class Program
{
    public enum Words
    {
        One,
        Two
    }

    static void Main(string[] args)
    {
        Words word = Words.Two;
        Console.WriteLine(word.ToString());
    }
}

In NETMF, this similar code prints “1”

public class Program
{
    public enum Words
    {
        One,
        Two
    }

    public static void Main()
    {
        Words word = Words.Two;
        Debug.Print(word.ToString());
    }
}

How do I make NETMF print “Two” instead of “1”?

The above are made up examples, in my real scenario, there are more enum values and I’d rather not do a giant switch case statement to translate the enum integer values back to human readable words. Thanks!

I ran into the same issue. Unfortunately, there currently doesn’t seem to be a way to get the string representation of an enum value. I think you’re stuck using a switch statement for now ???

Instead of switch a array lookup could be done:


   public enum Words
    {
      One,
      Two
    }

    string[] WordsStr = new string[2] {"One","Two"};

    public void Test()
    {
      Words word = Words.Two;
      Debug.Print(WordsStr[(int)word]);
    }


Instead of creating your array by hand, you can do:


public string[] GetStringArray(Type type)
{
    //use Reflection to get the fields in our enum
    FieldInfo[] info = type.GetFields();

    //new ArrayList to hold the values (will convert later)
    ArrayList fields = new ArrayList();

    //loop through all the fields
    foreach (FieldInfo fInfo in info)
    {
        //add each to our ArrayList
        fields.Add(fInfo.Name);
    }

    //now we convert to string array and return
    return (string[]) fields . ToArray ( typeof ( string ) );

  }

PS I don’t know why that link is appearing after ToArray, tried to add spaces but it’s still there :frowning:

Greetings

Thats a neat function :slight_smile:

I’m assume Enum.Parse() doesn’t exist in .NET MF?

hari,

if you do this in .NETMF you will get what you are asking for.

public enum Words
{
      One = 1,
      Two
}

NETMF is targeted for small devices. So the enums’ names are not there.
If you need them, you have to create your own string array with the names.

Check this one…a complete tutorial on Enum

Tomi

@ hari - You can do something like this:


public enum WebSocketState
{
	Initialized = 0, // initial state
	Connecting,
	Connected,
	Disconnecting,
	Disconnected
}

public static class WebSocketStateExtensions
{
	public static string Name(this WebSocketState webSocketState)
	{
		string text = "";
		switch (webSocketState)
		{
			case WebSocketState.Initialized:
				text = "Initialized";
				break;
			case WebSocketState.Connecting:
				text = "Connecting";
				break;
			case WebSocketState.Connected:
				text = "Connected";
				break;
			case WebSocketState.Disconnecting:
				text = "Disconnecting";
				break;
			case WebSocketState.Disconnected:
				text = "Disconnected";
				break;
		}
		return text;
	}
}

Then use it like this:


WebSocketState wss = WebSocketState.Connecting;
string currentState = "Current state: " + wss.Name();