C# Text Adventure: Lesson 8

Begin our Input/Output class

Now we're going to begin building up a class of static methods that have to do with either input at the console/input from files, or output to the console/output to files.

Go to your heroes directory. If you haven't already, go ahead and move testColors2.cs to your

/testcode/

subdirectory. If the file testColors2.exe still exists, delete it. Your project directory should now look like this:

Using Notepad++, create a new C# source code file named ConsoleInputOutput.cs in your heroes project directory.

Create the skeleton or boilerplate code per the following listing:

using Sytsem;
using System.IO;

public sealed class ConsoleIO{
    public static void conShow(string p){
        /* this method builds upon that of Console.Write() by looking for
           special character sequences with string p and translating them
           variously into (a) a space prepended to the passed string p,
           (b) a newline appended to string p, or (c) two newlines appended
           to string p. Lines 13 through 16 below can be shortened to the 
           single line of code shown in line 18 below: 
           
           Console.Write(" ");
           Console.Write("This is my string.");
           Console.Writeline();
           Console.Writeline();
           
           conShow("\\pThis is my string.\\d");

           */
    }
}

Save the source file. It should look like this in your Notepad++ editor (you may have to work on the indenting a bit if you copy/paste from the above listing):

Save ConsoleInputOutput.cs We won't bother compiling it to a DLL library yet, since it doesn't yet contain any useful methods.

Create source code file testInputOutput1.cs, and enter the following:

using System;

public sealed class TestInputOutput1
{
    public static void Main()
    {
        ConsoleColors.conWhiteOnBlack();
        Console.WriteLine();
        Console.Write(" Name: ");
        ConsoleColors.conFgCol("\\ge");
        Console.WriteLine("Bryan Mueller");
        ConsoleColors.conFgCol("\\wh");
        Console.Write(" Location: ");
        ConsoleColors.conFgCol("\\ge");
        Console.WriteLine("Kentucky");
        ConsoleColors.conFgCol("\\wh");
        Console.Write(" Age: ");
        ConsoleColors.conFgCol("\\ge");
        Console.WriteLine("45");
        ConsoleColors.conWhiteOnBlack();
    }
}

Save the test file, then switch to your command prompt and compile it with a reference to the ConsoleColors.dll library, like so:

csc /r:ConsoleColors.dll testInputOutput1.cs

If you had any errors with the compilation, check the screen shot below. Does your source code file match what you see in the following screen shot?

After successful compilation, testInputOutput1.exe produces the following output:

Now, I want to draw your attention to the fact that it took us a dozen lines of code (not counting the two calls to ConsoleColors.conWhiteOnBlack()) to produce just three lines of output. That's because I've formatted the output to make it easier on the eyes. Do you disagree that the output shown in the above screen shot is more pleasant and easily readable than the unformatted version show below?

Remember that ratio: 12 lines of source to produce 3 lines of formatted output. You'll note I put a space between the left edge of the Console viewing area and the beginning of each line of text. That looks better than when the text buts up against the left edge, agree? Now let's create our first method in ConsoleInputOutput.cs, and see if we can reduce that 12:3 (i.e, 4:1) ratio of source code lines to desired formatted output lines.

Return to ConsoleInputOutput.cs in Notepad++ and let's code a static method we'll name conShow(). Here is the listing of code you'll either type or copy/paste into your source code file:

    public static void conShow(string p)
    {
        /* This method is static so that we can invoke it without first instantiating an object.
            Just pass it the string you want the Console to show the user */

        bool doubleNewline = false;
        bool newline = false;
        bool prependSpace = false;

        if (p.EndsWith("\\n"))
        {
            newline = true;
            p = p.Substring(0, p.Length - 2);
        }
        if (p.StartsWith("\\p"))
        {
            prependSpace = true;
            p = p.Substring(2, p.Length - 2);
        }
        if (p.EndsWith("\\d"))
        {
            doubleNewline = true;
            p = p.Substring(0, p.Length - 2);
        }
        if (prependSpace)
        {
            p = " " + p;
        }
        if (newline)
        {
            Console.WriteLine(p);
        }
        else
        {
            if (doubleNewline)
            {
                Console.WriteLine(p);
                Console.WriteLine("");
            }
            else
            {
                Console.Write(p);
            }
        }
    }

Here's a screen shot of the ConsoleInputOutput.cs file after saving it with its first method. I've folded some of the code:

Let's add an overload of the conShow() method, one that takes two string parameters, the first specifying the text to be shown, the second specifying what foreground color to use when displaying it.

Go back into your ConsoleInputOutput.cs source file and add the following overloaded method:

    public static void conShow(string p, string fg)
    {
        ConsoleColors.conWhiteOnBlack();
        ConsoleColors.conFgCol(fg);
        conShow(p);
    }

Save the file, then switch to the command prompt and rebuild the DLL library. Do you remember how? Like this:

csc /t:library /r:ConsoleColors.dll ConsoleInputOutput.cs

Note: don't forget to reference ConsoleColors.dll when building ConsoleInputOutput.dll

Now let us return to our test file, testInputOutput1.cs. Bring it up in its Notepad++ tab and save it as (using File→Save As) testInputOutput2.cs. We're going to modify it and see if — by referencing the ConsoleInputOutput.dll library — we can mimic our first test file's output while using fewer lines of source code.

Once you get the newly saved ConsoleInputOutput.cs file (which now contains two methods) compiled to a DLL library, modify your testInputOutput2.cs file as follows:

using System;

public sealed class TestInputOutput2
{
    public static void Main()
    {
        ConsoleIO.conShow("\\n", "\\wh");
        ConsoleIO.conShow("\\pName: ", "\\wh");
        ConsoleIO.conShow("Bryan Mueller\\n", "\\ge");
        ConsoleIO.conShow("\\pLocation: ", "\\wh");
        ConsoleIO.conShow("Kentucky\\n", "\\ge");
        ConsoleIO.conShow("\\pAge: ", "\\wh");
        ConsoleIO.conShow("45\\n", "\\ge");
        ConsoleIO.conShow("", "\\wh");
    }
}

Save this file, then go to the command line and compile it like this:

csc /r:ConsoleInputOutput.dll testInputOutput2.cs

Upon successful compilation, run the program. Voila! The second version of our test program (testInputOutput2.cs) produces identical output to the first version (testInputOutput1.cs) but uses fewer lines of source code (see below):

When we compare the relevant section of source code from each version of our test application, we see the second version achieve in 8 lines what the first version took 14 lines to accomplish.

In the next lesson, we'll learn how to take a shortcut that makes the frequent recompilation of libraries and applications less tedious at the command line.

If you became unable to build a library or compile a test app, and you're sure that you are entering the correct info at the command line compiler, the problem is likely an error in one of your source files. If you need, you can download Lesson 8 source files here and extract them into your heroes directory.

Return to Lesson 7 | Proceed to Lesson 9

Return to Index


Author: kyrathasoft

The only programmer in Glens Fork, Ky.