Showing enumerated value

I have set up an enumeration:

 enum States : short { NONE, SYSTEMRUN, RESET, REWIND};
  States rrr = States.NONE0;

if i do a debug.pring on rrr, it gives a number 0,1,2…

how can you print out the enumerated text equiv (none, systemrun ,etc)?

you need to use a CASE statement and un-enumerate it. There’s no other way (been discussed before, search for enum and debug.print and you should find it)

Thnaks, I’m not sure I fully follow the example

so you would use it as:

Debug.print(GetStringArray(States, rrr)); ???

@ Hoyt, I deleted the code because it doesn’t work (sorry about that)

Then this is probably not right: Enum.ToString Method | Microsoft Learn or am I missing something

I also thought I saw Enum.ToString on the internet, but it doesn’t “pop up” in intellisense.

I see only Enum.Equals & Enum.ReferenceEquals

perhaps Brett has the only solution

This is a very bad stab in the dark, but cant one also use reflection?

GMOD, no, see this: http://www.tinyclr.com/forum/topic?id=2313

Ahh, my mistake. So this is another MF limitation.

Enum.ToSting() and reflection works just fine on full .NET…

Hi,
In order to get the string representation of the enum in NETMF you will have to create your own enum class using Fields.
here is how:


  public class MyOwnEnumType
    {
        /// <summary>
        /// holds the value
        /// </summary>
        private readonly object _value;
        #region Constructors

        /// <summary>
        /// 
        /// </summary>
        public MyOwnEnumType()
        {
            _value = "None";
        }

        /// <summary>
        /// To Hold Objects in the enum
        /// </summary>
        /// <param name="value"></param>
        public MyOwnEnumType(object value)
        {
            _value = value;
        }

        /// <summary>
        /// To Hold Strings in the enum
        /// </summary>
        /// <param name="value"></param>
        public MyOwnEnumType(string value)
        {
            _value = value;
        }

        /// <summary>
        /// To Hold integer in the enum
        /// </summary>
        /// <param name="value"></param>
        public MyOwnEnumType(int value)
        {
            _value = value;
        }

        /// <summary>
        /// To Hold GUID in the enum
        /// </summary>
        /// <param name="value"></param>
        public MyOwnEnumType(Guid value)
        {
            _value = value;
        }


//Add you own Types here....
        #endregion

   #region Overrides

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override string ToString()
        {
            return (_value != null) ? _value.ToString() : null;
        }

        #endregion

       #region Enums

        /// <summary>
        /// Add all your Enum below 
        /// public static readonly myEnumType myEnum1 = new myEnumType("myEnum Value");
        /// </summary>

        public static readonly MyOwnEnumType myString = new MyOwnEnumType("my Own String");
        /// <summary>
        /// 
        /// </summary>
        public static readonly MyOwnEnumType myInteger = new MyOwnEnumType(123456789);

        /// <summary>
        /// 
        /// </summary>
        public static readonly MyOwnEnumType Number = new MyOwnEnumType(1254);

        /// <summary>
        /// 
        /// </summary>
        public static readonly MyOwnEnumType myGuid = new MyOwnEnumType(Guid.NewGuid());

        #endregion

    }

// now to print the enum onto the output you would do this:

Debug.Print(MyOwnEnumType.myString);// would output: my Own String
 and so on...

Jay Jay…that’s way too much typin for me…this works:

public enum States: short { NONE0, SPLASH1, UCONFIGS2, FACTORY3, PORTS4};
static string[] stringStates = {"NONE0","SPLASH1","UCONFIGS2","FACTORY3","PORTS4"};

Debug.Print(" ==>Going to state " + stringStates[(int)nextState]);

I just hate having to create a duplicate string!

Hi,
What you have above is entirely different from what I’ve proposed… :wink:
My solution mimics the actual system enum without the enum restriction.

Pick the one you see it best fit your needs.

Cheers!!!