We have all had bugs that we have trouble finding! Finding bugs can involve a lot of detective work and so the more information we can gather about the problem the better. The diagnostics library is a very useful way of outputting runtime values and trapping problems.
Visual Studio provides powerful debugging capabilities. One of the most useful is the ability to break into the code manually or when there is a crash and to examine the variable values at the time of the crash. Simply hover your mouse over the variable. You can also use the call stack window to go back through the calls leading up to the break point and examine variables at those points.
To force a code break you can place a breakpoint in your code either by pressing F9, selecting Debug / Toggle Breakpoint from the menu or left clicking to the left of the code. Note that you must have the cursor on a line of code to be able to insert a break point.
When you now run your code the execution will stop at the breakpoint. You can now examine values or advance a line through the code (F10 - Step Over) and even step into functions (F11 - Step Into) or continue execution (F5 - Continue).
The .NET diagnostics library provides some useful functionality. To use it first of all import it via:
using System.Diagnostics;
To output text to the output pane in Visual Studio at run time you can now write:
Trace.WriteLine("Hello World");
Alternatively you can use Trace.Write and use the '\n' code to get a new line. When you run your code the text will appear in the output pane in Visual Studio. If you cannot see it make sure it is enabled from the Debug / Windows menu.
Outputting the value of a variable is much easier in C# than in C++. In C++ you had to use stringstreams and write a whole function, but in C# you can use the types ToString() function and add strings together to create an output e.g.
Trace.WriteLine("The current game time is "+gameTime.ToString());
The ToString function exists for basic types like int, float as well as the library types. For your own types you can implement a ToString function yourself if you wish.
Outputting text and variable values is a very useful way of observing what your code is doing during a loop without interrupting it.
Asserts are really useful because they can show up bugs before the bugs become a big problem. Basically an assert is you saying 'I assert that something is true at this point in my program'. For example:
void DrawToScreen(int screenX, int screenY)
{
Trace.Assert(screenX<screenWidth);
There are two further assert overloads you can call which allow you to put a string that will be displayed when the assert is triggered. e.g.
Trace.Assert(screenX<screenWidth,"Trying to draw outside the screen");
If an assert fails or your program crashes with some other error you can use the Visual Studio debugger to help find problems. When the error occurs you will have the option to press retry. Pressing this drops you into the debugger. Depending on your set-up you will have a number of different debug windows. To change which can be seen go to the Debug menu and select from the windows list.
The Call Stack window shows you the last few calls that were made prior to the crash. You can click on previous ones to go back through the code. At each point you can hover the mouse over variables to see what values they contain or use the watch window to type variables into. This way you can check to see what values variables have and deduce where the problem is. It takes a bit of detective work.