Skip to content

Simple C# Madlib (Part Two)

Iterating our simple C# Madlib code

This is part two in a series to create iterative versions of a Madlib in C#. If you haven't read through Simple C# Madlib (Part One), start there!

Next Iteration

A good practice is to keep the amount of code in our Main() function to a minimum. The first step we'll do in Part Two is make another class, Madlib, and move our code from Part One there.

New Class

We could add a new class as a separate file, or inside the same file we've been working with. For simplicity, let's look at adding it to the same file.

We'll add a class, Madlib, that will hold our variable declarations and a method that will hold the rest of our statements.

To make a new class you use the keyword class, and an identifier (a name for your class). You might need to add additional keywords depending on how your code will be accessed.

Example:

class ClassName
{
}

Identifier

For the identifier (name) you generally want something that is self-explanatory to someone else reading your code. A name that is intuitive. It's not a great idea to reuse the same identifier as something else you've named in your code. For the example I am going to rename the namespace as 'Game' and name the class 'Madlib'.

Modifiers

The optional keywords that you might use most often when first starting in C# are public and static. These are modifiers. Public allows other elements to access your code freely. Static allows you to access an element in a class without first creating an instance of it. Since we won't be creating an instance of this new class, we will make our Madlib class static.

  • Public is the most permissive level of access. "The type or member can be accessed by any other code in the same assembly or another assembly that references it." (MSDN)
  • Static indicates that we won't be making an instance of the class (Madlib).

Basic Structure

Here is the basic structure for our revised code:

using System;

namespace Game
{
    static class Madlib
    {
        //declare variables
        //Method with statements
    }
    class Program
    {
        static void Main()
        {
            //our statements are here now
        }
    }
}

Class Properties

Move the variable declaration statements to the Madlib class. Now that they are part of Madlib, they have a relationship with the class.

using System;

namespace Game
{
    static class Madlib
    {
        //declare variables
        static string Creature;
        static string Luminous;
        static string Ghastly;
        static string Spectral;
        static string Countryman;
        static string Farrier;
        static string Farmer;
        static string Dreadful;
        static string Apparition;
        static string Hound;
        static string Story;

    }
    class Program
    {
        static void Main()
        {
            //variable declarations were here
            //our other statements 
        }
    }
}

Method Creation

Next create a method to hold the rest of our code. A method is a function that is part of a class.

Our new method, Run(), will have three key words, public, static and void, and won't take any parameters so the parentheses will be empty.

We've already talked about public and static. Public is the most permissive level of access. The next keyword, static, indicates that we won't be making an instance of the class (Madlib).

The third keyword, void, indicates that our function won't be returning a value.

Here is the basic form:

public static void Run()
        {
            //statements
        }

Code progress

using System;

namespace Game
{
    static class Madlib
    {
        //declare variables
        static string Creature;
        static string Luminous;
        static string Ghastly;
        static string Spectral;
        static string Countryman;
        static string Farrier;
        static string Farmer;
        static string Dreadful;
        static string Apparition;
        static string Hound;
        static string Story;

        public static void Run()
        {
            //write header and instructions
            Console.WriteLine("-------");
            Console.WriteLine("Madlib!");
            Console.WriteLine("-------");

            //ask player to enter words
            Console.Write("Please enter a noun: ");
            Creature = Console.ReadLine();

            Console.Write("Please enter an adjective: ");
            Luminous = Console.ReadLine();

            Console.Write("Please enter an adjective: ");
            Ghastly = Console.ReadLine();

            Console.Write("Please enter an adjective: ");
            Spectral = Console.ReadLine();

            Console.Write("Please enter an occupation: ");
            Countryman = Console.ReadLine();

            Console.Write("Please enter an occupation: ");
            Farrier = Console.ReadLine();

            Console.Write("Please enter an occupation: ");
            Farmer = Console.ReadLine();

            Console.Write("Please enter an adjective: ");
            Dreadful = Console.ReadLine();

            Console.Write("Please enter a noun: ");
            Apparition = Console.ReadLine();

            Console.Write("Please enter a noun: ");
            Hound = Console.ReadLine();

            //write out story
            Story = "They all agreed that it was a huge " + Creature + ", " + Luminous + ", " + Ghastly + ", and " + Spectral + ". I have cross-examined these men, one of them a hard-headed " + Countryman + ", one a " + Farrier + ", and one a moorland " + Farmer + ", who all tell the same story of this " + Dreadful + " " + Apparition + ", exactly corresponding to the " + Hound + " of the legend.";
            Console.WriteLine(Story);

            //keep window open and prompt for exit
            Console.WriteLine("Press enter to exit");
            Console.ReadKey();
        }


    }
    class Program
    {
        static void Main()
        {
            //original statements were here
        }
    }
}

Calling Methods

To trigger a function or method is called 'invoking' or 'calling' it.

Now we need to put something inside of Main() to call our Madlib method Run(). When our application first starts, Main() is the entry point. The statements inside of Main() will run line by line. When we call our method Run(), the application will move to the top of the Run() method's code block and execute each line inside of it before returning to the next line in Main(). You can watch your application 'step' through the code by using the F11 key (see Problem Solving for more information).

Because our Madlib class is static, we can reference it like this:

Madlib.Run();

Code progress

Your code should look similar to:

using System;

namespace Game
{
    static class Madlib
    {
        //declare variables
        static string Creature;
        static string Luminous;
        static string Ghastly;
        static string Spectral;
        static string Countryman;
        static string Farrier;
        static string Farmer;
        static string Dreadful;
        static string Apparition;
        static string Hound;
        static string Story;

        public static void Run()
        {
            //write header and instructions
            Console.WriteLine("-------");
            Console.WriteLine("Madlib!");
            Console.WriteLine("-------");

            //ask player to enter words
            Console.Write("Please enter a noun: ");
            Creature = Console.ReadLine();

            Console.Write("Please enter an adjective: ");
            Luminous = Console.ReadLine();

            Console.Write("Please enter an adjective: ");
            Ghastly = Console.ReadLine();

            Console.Write("Please enter an adjective: ");
            Spectral = Console.ReadLine();

            Console.Write("Please enter an occupation: ");
            Countryman = Console.ReadLine();

            Console.Write("Please enter an occupation: ");
            Farrier = Console.ReadLine();

            Console.Write("Please enter an occupation: ");
            Farmer = Console.ReadLine();

            Console.Write("Please enter an adjective: ");
            Dreadful = Console.ReadLine();

            Console.Write("Please enter a noun: ");
            Apparition = Console.ReadLine();

            Console.Write("Please enter a noun: ");
            Hound = Console.ReadLine();

            //write out story
            Story = "They all agreed that it was a huge " + Creature + ", " + Luminous + ", " + Ghastly + ", and " + Spectral + ". I have cross-examined these men, one of them a hard-headed " + Countryman + ", one a " + Farrier + ", and one a moorland " + Farmer + ", who all tell the same story of this " + Dreadful + " " + Apparition + ", exactly corresponding to the " + Hound + " of the legend.";
            Console.WriteLine(Story);

            //keep window open and prompt for exit
            Console.WriteLine("Press enter to exit");
            Console.ReadKey();
        }


    }
    class Program
    {
        static void Main()
        {
            Madlib.Run();
        }
    }
}

Test your application to make sure you don't have any errors.

Refactoring

This has made our code much better in that we've moved our statements out of the Main() function and into a custom class we've built. We could improve our code more, though, with refactoring.

Refactoring means we'll change the way it is structured internally. When we run our Madlib application again, the output to the console window will still look the same. To someone who can't see the code, they won't know we've updated it; we are modifying the internal structure without changing the external behavior.

One way to look at what is happening in our application is to break it down into these operations:

  • Introduce the madlib with a title
  • Ask the player for words, and let them enter them. Words are saved as variables.
  • Write out the story using the words the player entered.
  • Let the player exit the application

Each of these operations could be a separate method (function). After defining them, we will call the methods inside of Run().

Of course, there are many ways to break down what is happening in the program - this is just one possibility.

Decide on how you would like to improve your Madlib. You can use the bullet points above, or create your own breakdown. Make the changes in your code and test it to see how it runs.

Code Progress

Below is an example of code progress based on the operations listed above. Yours will look different if you've chosen your own way of grouping related statements into functions.

using System;

namespace Game
{
    static class Madlib
    {
        //declare variables
        static string Creature;
        static string Luminous;
        static string Ghastly;
        static string Spectral;
        static string Countryman;
        static string Farrier;
        static string Farmer;
        static string Dreadful;
        static string Apparition;
        static string Hound;
        static string Story;

        public static void Run()
        {
            Start();
            GetWords();
            WriteStory();
            End();
        }

        static void Start()
        {
            //write header and instructions
            Console.WriteLine("-------");
            Console.WriteLine("Madlib!");
            Console.WriteLine("-------");
            
        }

        static void GetWords()
        {
            //ask player to enter words
            Console.Write("Please enter a noun: ");
            Creature = Console.ReadLine();

            Console.Write("Please enter an adjective: ");
            Luminous = Console.ReadLine();

            Console.Write("Please enter an adjective: ");
            Ghastly = Console.ReadLine();

            Console.Write("Please enter an adjective: ");
            Spectral = Console.ReadLine();

            Console.Write("Please enter an occupation: ");
            Countryman = Console.ReadLine();

            Console.Write("Please enter an occupation: ");
            Farrier = Console.ReadLine();

            Console.Write("Please enter an occupation: ");
            Farmer = Console.ReadLine();

            Console.Write("Please enter an adjective: ");
            Dreadful = Console.ReadLine();

            Console.Write("Please enter a noun: ");
            Apparition = Console.ReadLine();

            Console.Write("Please enter a noun: ");
            Hound = Console.ReadLine();

        }

        static void WriteStory()
        {
            //write out story
            Story = "They all agreed that it was a huge " + Creature + ", " + Luminous + ", " + Ghastly + ", and " + Spectral + ". I have cross-examined these men, one of them a hard-headed " + Countryman + ", one a " + Farrier + ", and one a moorland " + Farmer + ", who all tell the same story of this " + Dreadful + " " + Apparition + ", exactly corresponding to the " + Hound + " of the legend.";
            Console.WriteLine(Story);
            
        }
        static void End()
        {
            //keep window open and prompt for exit
            Console.WriteLine("Press enter to exit");
            Console.ReadKey();
        }

    }
    class Program
    {
        static void Main()
        {
            Madlib.Run();
        }
    }
}

In part three we will use arrays and a loop to streamline our code! Simple C# Madlib (Part Three)