3
\$\begingroup\$

I've recently learned the concept of enum and method calling with the enum. From what I learned I've done this simple snippet which changes the background color of the console with the help of enum and method.

    public static void SetColor(RanngDe R){
        switch (R){
            case RanngDe.Blue:
                 Console.BackgroundColor = ConsoleColor.Blue;
                 break;

            case RanngDe.Green:
                 Console.BackgroundColor = ConsoleColor.Green;
                 break;
            default:
                 Console.BackgroundColor = ConsoleColor.Yellow;
                 break;
        }   
    }

// Enum Declaration
public enum RanngDe{
    Blue=0,
    Green=1,
    Yellow=3
}

I'm calling RanngDe method in the main method in switch ...case block as menu driven. I know, I've used switch ...case in void SetColor(RanngDe R) method, which is my major concern, because this makes my code redundant as I'm calling it in the menu driven program.

Is this approach acceptable as good practice? If not then, how should I make this better?

\$\endgroup\$
  • 1
    \$\begingroup\$ Without seeing the rest of the program it is hard to say, but an alternate method would be to use a dictionary which would remove the switch statement and make it a simple assignment statement. \$\endgroup\$ – pacmaninbw Apr 25 '17 at 21:12
  • 1
    \$\begingroup\$ Why not use ConsoleColor directly, instead of creating a RanngDe enum? Is it really important to limit the parameter to those three colors? \$\endgroup\$ – 200_success Apr 25 '17 at 23:31
6
\$\begingroup\$

As has been mentioned using a Dictionary<RanngDe,Color> will make your code much simpler:

public enum RanngDe
{
    Blue = 0,
    Green = 1,
    Yellow = 3
}
static Dictionary<RanngDe, ConsoleColor> colors = new Dictionary<RanngDe, ConsoleColor>()
{
    {RanngDe.Blue,ConsoleColor.Blue },
    {RanngDe.Green,ConsoleColor.Green },
    {RanngDe.Yellow,ConsoleColor.Yellow }
};
public static void SetColor(RanngDe R)
{
    Console.BackgroundColor = colors[R];
}

Depending on your implementation you might be able to do away with the method completely and just use the assignment.

I came up with another approach. If you change the values of Ranngde to be the same as the ConsoleColor enum, you can cast the Ranngde value to ConsoleColor:

public enum RanngDe
{
    Blue = ConsoleColor.Blue,
    Green = ConsoleColor.Green,
    Yellow = ConsoleColor.Yellow
}
public static void SetColor(RanngDe R)
{
    Console.BackgroundColor = (ConsoleColor)R;
}
\$\endgroup\$
  • 2
    \$\begingroup\$ It would be even nicer if you defined the enum values as Blue = ConsoleColor.Blue instead of using magic numbers. \$\endgroup\$ – t3chb0t Apr 26 '17 at 4:13
  • 1
    \$\begingroup\$ Yep. It is done \$\endgroup\$ – tinstaafl Apr 26 '17 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.