Dvir
Dvir

Reputation: 17

input consoleColor

How can I get an input of consoleColor in c# console-application?

I tried a few options, and none of them was working:

ConsoleColor c = ConsoleColor.parse(Console.ReadLine());
ConsoleColor c = Console.ReadLine();
ConsoleColor c = (ConsoleColor)Console.ReadLine();

Upvotes: 1

Views: 943

Answers (2)

RBT
RBT

Reputation: 25945

One rather convenient way of getting such a color input would be through integer values. Users always want to type minimum. So, you can have integer/number based menu which correspond to enum values maintained internally by ConsoleColor enum as below:

public enum ConsoleColor
    {
        //
        // Summary:
        //     The color black.
        Black = 0,
        //
        // Summary:
        //     The color dark blue.
        DarkBlue = 1,
        //
        // Summary:
        //     The color dark green.
        DarkGreen = 2,
        //
        // Summary:
        //     The color dark cyan (dark blue-green).
        DarkCyan = 3,
        //
        // Summary:
        //     The color dark red.
        DarkRed = 4,
        //
        // Summary:
        //     The color dark magenta (dark purplish-red).
        DarkMagenta = 5,
        //
        // Summary:
        //     The color dark yellow (ochre).
        DarkYellow = 6,
        //
        // Summary:
        //     The color gray.
        Gray = 7,
        //
        // Summary:
        //     The color dark gray.
        DarkGray = 8,
        //
        // Summary:
        //     The color blue.
        Blue = 9,
        //
        // Summary:
        //     The color green.
        Green = 10,
        //
        // Summary:
        //     The color cyan (blue-green).
        Cyan = 11,
        //
        // Summary:
        //     The color red.
        Red = 12,
        //
        // Summary:
        //     The color magenta (purplish-red).
        Magenta = 13,
        //
        // Summary:
        //     The color yellow.
        Yellow = 14,
        //
        // Summary:
        //     The color white.
        White = 15
    }

So on the console you show a menu to the user in something similar order:

Choose:
0 For Black
1 For DarkBlue
2 For DarkGreen
..and so on

Now simply read the integer input from console and use tryPparse API. tryParse API is equally helpful for parsing integral values as well and converting them into enum values:

var userInput = Console.ReadLine();//this will be an integer
ConsoleColor color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor),userInput );

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236268

Console.ReadLine() returns string input from user. ConsoleColor is enumeration. You need to parse input string to get enumeration value:

ConsoleColor color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), Console.ReadLine());

Also keep in mind that user can input value which is not correct console color name. In that case it's better to try parse input:

ConsoleColor color;
if (!Enum.TryParse(Console.ReadLine(), true, out color))
    Console.WriteLine("You have entered incorrect color name");

Upvotes: 3

Related Questions