C# Text Adventure: Lesson 6

Console Coloring Encapsulation

In this lesson, we are going to create a class that encapsulates coloring the output of C# console programs. Because let's be honest: if we're going to create a text adventure game, it would be darn handy if we could easily color our output.

Think about this: why might it be a good idea to put all of our console-color manipulating code in one single source file? And, likewise, why might it be wise to put all of our data input/output code in its own source file? Because that's the plan: before we actually begin coding the text adventure itself, we'll put our console coloring code into a library, and our input/output code in another library.

I don't know if you, the reader, have ever done any mid-size C# projects before (say 2-6K lines of source code), but I have, and I can attest that logically organizing related code into separate source files makes finding a particular method so much easier than if you had one giant source file. And dividing your source code into smaller files based upon topic/function their code addresses lends itself to the creation of useful libraries of code. Libraries in C# are often compiled into DLL files. In fact, DLL stands for Dynamic Link Library, a bundle of methods that can be accessed when needed by various applications.

So... to begin.

Fire up Notepad++, or whatever other editor you chose, and create a new file, saving it as ConsoleColors.cs in our heroes project folder. Be sure to Save As type "C# Source File":

After saving this (currently empty) source file, we'll create a public sealed class to hold the code we're about to create. It needs to be public so that it will be visible to code elsewhere, code that wants to utilize this class' methods. It's sealed because it's only going to contain static methods. No other classes will inherit from ConsoleColors.cs.

Now, let's add a method to this class. We'll name it conWhiteOnBlack(). Based on that name, can you guess what it's purpose will be? Yes. It will set the console to use a white-on-black "scheme", white foreground on a black background:

You may not think this method seems needed, but believe me when I say that — once we begin coding our text adventure — we'll be changing colors frequently, and will often need to reset the console to its normal white-on-black appearance.

Why note just write the following each and every time we need to do this:

Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Black;

I suppose that'd be okay, if we were only going to do this occasionally. But we'll be doing it frequently, and so we can achieve a substantial savings in source-code size and our typing effort, if we can simply code the same functionality with a single line of code:

ConsoleColors.conWhiteOnBlack()

Save the file, then switch over to your command prompt. Let's create a DLL from this source code file:

At the command prompt enter the following. Be sure to type it exactly as shown below, then press Enter:

csc /t:library ConsoleColors.cs

If you don't get an error message, enter the

dir

command after building the DLL library:

As indicated by the yellow arrow in the above screen shot, our DLL library (scant as it is, currently), was created. Now let's create a console application to reference that DLL, in order to test it:

Save the above as "testColors1.cs" in the heroes directory. Now, we're going to first try compiling it without referencing the DLL that contains the

conWhiteOnBlack()

method.

The CS 103 error message issued by the compiler alerts us that our test code has no idea what we're referring to with the following line of code:

ConsoleColors.conWhiteOnBlack()

This is because we didn't reference the ConsoleColors.dll file when we compiled testColors1.cs. Let's try again, and this time we'll use the /r switch to reference our DLL library. Note the use of the referencing switch and the successful compilation shown below:

Run the newly created application, testColor1.exe, at the command line and observe the output. Our DLL has encapsulated the two calls normally needed, and a single line of code now suffices:

We're done with that particular test, so create a subdirectory in heroes named testcode. Delete the testColors1.exe file in the heroes directory and move testColors1.cs into the *testcode subdirectory. We'll be keeping any test code there, so it doesn't clutter up our main heroes directory:

Your heroes directory should now look like this:

In our next lesson, we'll expand ConsoleColors.cs, create an updated DLL from it, and test it. If you're bored so far, that's understandable. Just know that the good stuff is coming about 3-6 lessons from now, once we've done our prep work.

Return to Lesson 5 | Proceed to Lesson 7

Return to Index


Author: kyrathasoft

The only programmer in Glens Fork, Ky.