Contents

Troubleshooting

As you work through the Katas, you’ll probably make mistakes that lead to errors appearing in the Command Prompt. This chapter is designed to help you fix these errors. This process is known as ‘debugging’.

Errors in the Command Prompt

First, don’t panic when you run your code and you encounter errors - they’re bound to come up and they look scary at first. However, by carefully reading each message you can find the root cause of the problem.

Here’s an example of how to debug a sample error. Say you had a program that looks like this:

Program.cs:

using System;

namespace Debugging
{
    class Program
    {
        static void Main(string[] args)
        {
            string myString = "Hello World";
            Console.WriteLine(mySting);
        }
    }
}

If you ran this, you would get the following error message:

Program.cs(10,31): error CS0103: The name 'mySting' does not exist in the current context [C:\Code\Debugging.csproj]
Program.cs(9,20): warning CS0219: The variable 'myString' is assigned but its value is never used [C:\Code\Debugging.csproj]

The build failed. Please fix the build errors and run again.

Let’s break this down. There are two errors here. The first one is:

Program.cs(10,31): error CS0103: The name 'mySting' does not exist in the current context [C:\Code\Debugging.csproj]

So, reading this carefully you can see there is a problem in Program.cs. Inside the brackets are the line number, and the number of characters across that the error has occurred. In this case, we have (10,31), which means line 10, 31 characters across.

Next is the error code. It’s error CS0103. This is a unique identifier which is used to categorize the type of error encountered. This is usually not helpful - but for trickier bugs, it can be useful to note this down for reference if you need to search the official C# documentation for help.

Finally there’s a ‘friendly’ description of the problem. ‘Friendly’ is sort of programmer short hand for ‘message that can actually be read by a human instead of being a bunch of computery-gobledegook’. In this case it’s The name 'mySting' does not exist in the current context.

So, going to line 10, of the program you have the following code:

Console.WriteLine(mySting);

Whats wrong here? The program is printing the value of mySting and.. Oh! It ought to be myString, not mySting. What looked at first to be a complicated problem was actually just a typo!

Fixing the typo makes my program work. So it turns out the second error:

Program.cs(9,20): warning CS0219: The variable 'myString' is assigned but its value is never used [C:\Code\Debugging.csproj]

Was actually just because myString technically wasn’t being used in my program - because the place you wanted to use myString, you had a typo and wrote mySting instead!

Console.WriteLine

Console.WriteLine is your friend when debugging your code. If you’ve fixed all your errors, but your code is producing incorrect outputs, then you need a way of understanding what information your code is working with. Outputting the values of variables with Console.WriteLine is the way to go about this.

Say you had a program like this:

int rateOfPay = 12;
int overtimeRateOfPay = rateOfPay * 1.5;
int finalPay = overtimeRateOfPay * 12;
Console.WriteLine(finalPay);

If you wanted to understand why the output was wrong, you could simply output the value of overtimeRateOfPay:

int rateOfPay = 12;
int overtimeRateOfPay = rateOfPay * 1.5;
Console.WriteLine(overtimeRateOfPay);
int finalPay = overtimeRateOfPay * 12;
Console.WriteLine(finalPay);

This might be enough to help you understand why finalPay, and hence the output of the program, is incorrect.

Google

If all else fails, just Google the error message you’re getting. This is usually enough to help you find out what’s going wrong with your code.

Now, an important thing to remember is that the people on Google don’t know your specific problem - you’ll only ever find generalized answers to general problems. This is especially true if your code can run, but is producing incorrect results. Bear that in mind as you use it to help you when you’re stuck. That said, it’s usually enough to get you on the right track.

Copyright Mikiel Agutu 2019