1

While trying to learn C# Im trying to play with Enum and switch statement. how can I pass the user input into a variable in order to let him select one of the options in an enum?

what is the right way to pass the user input?

my code looks like this:

using System;

namespace switchDemo
{
    class Program
    {
        enum Movies
        {
            LOTR,
            Starwars,
            Matirx
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Choose the best movie (LOTR, Starwars, Matirx): ");
            Movies bestMovie = Convert.ToString(Console.ReadLine());

            //Movies bestMovie = Movies.LOTR;

            switch (bestMovie)
            {
                case Movies.LOTR:
                    Console.WriteLine("thats correct - LOTR");
                    break;
                case Movies.Matirx:
                    Console.WriteLine("Martix is not the best");
                    break;
                case Movies.Starwars:
                    Console.WriteLine("StarWars is 2nd best");
                    break;
                default:
                    Console.WriteLine("choose an option");
                    break;
            }

            Console.WriteLine("Hello World!");
        }
    }
}

1 Answer 1

5

You can use the Enum.TryParse method to convert a string to an enum:

if (Enum.TryParse<Movies>(Console.ReadLine(), ignoreCase: true, out var bestMovie)) {
    switch (bestMovie) {
        case Movies.LOTR:
            break;
        case Movies.Starwars:
            break;
        case Movies.Matirx:
            break;
        default:
            break;
    }
} else {
    Console.WriteLine("Unknown movie");
}

I understand this is only an exercise, but since the set of available movies is likely to change, formulating it as enum is not the best option. In a real-life application you would keep the movie information in a data-base (either a "real" one or a XML-file, text-file etc.). It is best to use enums for constant things. E.g.

enum DisplayMode
{
    Basic,
    Advanced
}

Since the corresponding display routines are hard-coded, it makes sense to also hard-code the modes.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Oliver, so do I have to use an if statement in this case? Im just learning here its not a real life case. thanks for answering.
Alternatively, you can also use the Enum.Parse Method without if statement, but if the user enters an unknown movie name this method will throw an exception. It is safer to use TryParse in conjunction with if to be able to handle the case where an unknown movie is entered. The same applies when the user enters numbers or dates. User entries have always to be treated as potentially wrong and must be validated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.