locked
How to get the name of enum? RRS feed

  • Question

  • For example, I've the following enum, how I can get "OK" or "YesNo" string from "Mode" enum to be used in the string.format()?

        public enum Mode
        {
            Ok,
            YesNo,
        }

    Your help is greatly appreicated.

    Thanks.

    Thursday, March 15, 2012 4:10 PM

Answers

  • Here is an example to get all values:

    public enum Mode
    {
        Ok,
        YesNo,
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            foreach (string m in Enum.GetNames(typeof(Mode)))
            {
                Console.WriteLine("Value: {0}", m);
    
            }
        }
    }

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Marked as answer by Wood-MSDN Thursday, March 15, 2012 4:50 PM
    Thursday, March 15, 2012 4:27 PM
  • I am not usre what exactly you are looking for...

    But if you need to get the string name of an enum value, you can use the static methods of the Enum class...

    e.g to get the string for the OK enum value,

    you can use

    string enumValue = Enum.GetName(typeof(Mode), Mode.Ok); // This will treturn a string OK

    To enumerate through all the value of the enum, use

    string[] enumValues = Enum.GetNames(typeof(Mode));

    Regards

    Andy


    andypi

    • Marked as answer by Wood-MSDN Thursday, March 15, 2012 4:50 PM
    Thursday, March 15, 2012 4:34 PM

All replies

  • I'm not sure whats the problem or maybe I don't understand it but here should be the solution:

                Console.WriteLine(string.Format("{0} - OK ------ {1} Yes/No"Mode.Ok, Mode.YesNo));
    

    Thursday, March 15, 2012 4:21 PM
  • Or you can use enums index (1st name has index 0, 2nd one index 1 and so on - if you DO NOT specify it differently!!!)

    So:

     Console.WriteLine("The 1st value of the ModeEnum is {0}", Enum.GetName(typeof(Mode), 0));
     Console.WriteLine("The 1st value of the ModeEnum is {0}", Enum.GetName(typeof(Mode), 1));
    //result will be:
    //The 1st value of the ModeEnum is Ok
    //The 1st value of the ModeEnum is YesNo

    Hopr it helps.

    bye


    Mitja

    Thursday, March 15, 2012 4:26 PM
  • Or maybe:

    Mode m = Mode.Ok;
    Console.WriteLine ("Selected Enum is {0}.", m.ToString());


    Mitja

    • Proposed as answer by Kuacci Thursday, March 15, 2012 4:36 PM
    Thursday, March 15, 2012 4:27 PM
  • Here is an example to get all values:

    public enum Mode
    {
        Ok,
        YesNo,
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            foreach (string m in Enum.GetNames(typeof(Mode)))
            {
                Console.WriteLine("Value: {0}", m);
    
            }
        }
    }

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Marked as answer by Wood-MSDN Thursday, March 15, 2012 4:50 PM
    Thursday, March 15, 2012 4:27 PM
  • I am not usre what exactly you are looking for...

    But if you need to get the string name of an enum value, you can use the static methods of the Enum class...

    e.g to get the string for the OK enum value,

    you can use

    string enumValue = Enum.GetName(typeof(Mode), Mode.Ok); // This will treturn a string OK

    To enumerate through all the value of the enum, use

    string[] enumValues = Enum.GetNames(typeof(Mode));

    Regards

    Andy


    andypi

    • Marked as answer by Wood-MSDN Thursday, March 15, 2012 4:50 PM
    Thursday, March 15, 2012 4:34 PM
  • Thanks all for the quick response...
    Thursday, March 15, 2012 4:51 PM