C# Text Adventure: Lesson 7

Expanding ConsoleColors.cs

In this lesson we'll expand our existing ConsoleColors.cs source file. Currently, it contains a single static method named conWhiteOnBlack(), which sets the console's foreground color to White and background color to Black.

Open the source file in Notepad++ and let's add a static method that accepts a short string parameter and that sets a Foreground color based upon that parameter. This method will include all of the colors defined for the Console class. It's a lengthy method, so if you don't want to type it all in manually (which I still suggest you do), you can copy and paste it from the following listing:

    public static void conFgCol(string p)
    {
        switch (p)
        {
            case "\\bk":
                Console.ForegroundColor = ConsoleColor.Black;
                break;
            case "\\cy":
                Console.ForegroundColor = ConsoleColor.Cyan;
                break;
            case "\\db":
                Console.ForegroundColor = ConsoleColor.DarkBlue;
                break;
            case "\\dc":
                Console.ForegroundColor = ConsoleColor.DarkCyan;
                break;
            case "\\dy":
                Console.ForegroundColor = ConsoleColor.DarkGray;
                break;
            case "\\dg":
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                break;
            case "\\dm":
                Console.ForegroundColor = ConsoleColor.DarkMagenta;
                break;
            case "\\dr":
                Console.ForegroundColor = ConsoleColor.DarkRed;
                break;
            case "\\de":
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                break;
            case "\\ye":
                Console.ForegroundColor = ConsoleColor.Yellow;
                break;
            case "\\gr":
                Console.ForegroundColor = ConsoleColor.Gray;
                break;
            case "\\rd":
                Console.ForegroundColor = ConsoleColor.Red;
                break;
            case "\\ge":
                Console.ForegroundColor = ConsoleColor.Green;
                break;
            case "\\bl":
                Console.ForegroundColor = ConsoleColor.Blue;
                break;
            case "\\ma":
                Console.ForegroundColor = ConsoleColor.Magenta;
                break;
            case "\\wh":
                Console.ForegroundColor = ConsoleColor.White;
                break;
            default:
                Console.ForegroundColor = ConsoleColor.White;
                break;
        }
    }

Below, I show the source code file as it now exists. I have folded the switch statement:

Let's add one more method. One that reverses the usual white-on-black color scheme to black-on-white instead:

Save the source code file and then let's switch to the command line and rebuild our DLL from it using the following at the prompt. Note that before rebuilding the DLL, the library's size is about 3,072 bytes on disk:

csc /t:library ConsoleColors.cs

After rebuilding the library, it's size on disk is about 4,096 bytes. The DLL file's size has increased because the source code has increased.

Create a new console program to test our rebuilt library. Name it testColors2.cs and save it in our heroes project directory:

Enter source code into the test program using Notepad++, based on the following screen shot:

Save the source code for this test program and go to your command prompt. Compile using the following:

csc /r:ConsoleColors.dll testColors2.cs

At the command prompt, run the newly created executable by entering the following:

testColors2

Here's the output (note that I typed some text at the command prompt after the test program exited, just to demonstrate the effect on the console of reversing the usual foreground/background color scheme):

Modify the test program source code file as follows:

using System;

public sealed class TestCC2
{
    public static void Main()
    {
        ConsoleColors.conWhiteOnBlack();
        Console.WriteLine("I'm the usual white-on-black...");
        ConsoleColors.selfTest();
        Console.WriteLine("But press a key, and I'll change to black-on-white...");
        Console.ReadKey();
        ConsoleColors.conBlackOnWhite();
        Console.WriteLine("Now I'm black-on-white!");
        Console.WriteLine("Press any key to exit app...");
        Console.ReadKey();
        ConsoleColors.conWhiteOnBlack();
    }
}

:

Save, and recompile at the command line. Now our test program resets the console to its usual color scheme before exiting.

Now, open the ConsoleColors.cs source file, and add the following method to it:

    public static void selfTest()
    {
        conWhiteOnBlack();
        Console.WriteLine();
        Console.WriteLine("Now testing colors...");
        conFgCol("\\cy");
        Console.Write("cyan, ");
        conFgCol("\\db");
        Console.Write("dark blue, ");
        conFgCol("\\dc");
        Console.Write("dark cyan, ");
        conFgCol("\\dy");
        Console.Write("dark gray");
        Console.WriteLine();
        Console.WriteLine();
    }

Save the source file, then go to the command prompt and rebuild the DLL library using the following:

csc /t:library ConsoleColors.cs

And now, having rebuilt the DLL, go back to the test program, testColors2.cs and ensure that the source code conforms to the following listing:

using System;

public sealed class TestCC2
{
    public static void Main()
    {
        ConsoleColors.conWhiteOnBlack();
        Console.WriteLine("I'm the usual white-on-black...");
        ConsoleColors.selfTest();
        Console.WriteLine("But press a key, and I'll change to black-on-white...");
        Console.ReadKey();
        ConsoleColors.conBlackOnWhite();
        Console.WriteLine("Now I'm black-on-white!");
        Console.WriteLine("Press any key to exit app...");
        Console.ReadKey();
        ConsoleColors.conWhiteOnBlack();
    }
}

Save the testColor2.cs source code file, then return to the command prompt and compile it as follows:

csc /r:ConsoleColors.dll testColors2.cs

Run the test program at the command line. Cool, eh? On your own, expanded the selfTest() method in ConsoleColors.cs so it tests all of the cases in conFgCol(). Then rebuild the DLL, recompile the test program, and run it.

That That's it for Lesson 7. You can now move the testColor2.cs source file to your

/testcode/

subdirectory.

In lesson 8 we'll begin work on our class that will handle input and output at the console.

Return to Lesson 6 | Proceed to Lesson 8

Return to Index


Author: kyrathasoft

The only programmer in Glens Fork, Ky.