0

I simply need to use enum with entered number, but I don't know how to convert. Do I have to create an additional variable of int and than convert?

        public enum Actions
        {
             delete,
             add,
             edit
        };
        Actions ac = Actions.Parse(Concole.Readline());
        switch (ac)
        {
            case Actions.delete:
                int k = int.Parse(Console.ReadLine());
                Delete(flights[k]);
                Console.WriteLine("");
                break;
            case 2:
                Add();
                Console.WriteLine("");
                break;
            case 3:
                Edit();
                Console.WriteLine("");
                break;
            default:
                Console.WriteLine("Incorrect number!");
                break;
        }
  • 2
    Why are you mixing `int` and `enum` values in the case(s)? – maccettura Feb 24 '20 at 18:50
  • So your question is you want the users to enter a number and have that correspond to an enum value? – maccettura Feb 24 '20 at 18:55
  • The question "how to convert an enum to an int" has already been answered [here](https://stackoverflow.com/questions/943398/get-int-value-from-enum-in-c-sharp). It's not clear why you have a mix of enum and int in your switch cases, however. – Rufus L Feb 24 '20 at 18:57
  • Yes, I want to use enum for my switch-case. User has to enter the number 1-3 and choose the action which he wants to do – born2drum 2 Feb 24 '20 at 18:59
  • i think it is obvious from his code that he wants to cast the console input to an enum. he just dont know that console gives him a string – Welcor Feb 24 '20 at 19:01

2 Answers2

0

You can take in a string and convert it to Enum using Enum.Parse

Actions ac = (Actions)Enum.Parse(typeof(Actions), Console.ReadLine());

// THen use switch on ac.
    switch (ac)
    {
        case Actions.add:
            // do something here
            break;

        case Actions.delete:
            // do something here
            break;
        default:
            throw new ApplicationException("Error");
    }

Enums, by default, starts with 0. If you want to start with a 1, then assign the int values

public enum Actions
        {
             delete = 1,
             add = 2,
             edit = 3
        };
Jawad
  • 11,028
  • 3
  • 24
  • 37
-1

from this answer: https://stackoverflow.com/a/29485/4394435

public enum Actions
{
    delete,  // 0
    add,     // 1
    edit     // 2, you can change those by setting them like this: edit = 2
};

class Program
{
    static void Main(string[] args)
    {
        Actions ac = (Actions) Enum.Parse(typeof(Actions), Console.ReadLine());
        switch (ac)
        {
            case Actions.delete:
                int k = int.Parse(Console.ReadLine());
                Delete(flights[k]);
                Console.WriteLine("");
                break;
            case Actions.add:
                Add();
                Console.WriteLine("");
                break;
            case Actions.edit:
                Edit();
                Console.WriteLine("");
                break;
            default:
                Console.WriteLine("Incorrect number!");
                break;
        }
    }
}
Welcor
  • 2,431
  • 21
  • 32