Console Windows

Overview

There's a lot of beginning XNA game developers who have done a lot of work using programs involving only a console window. For those of you who are like this, you are probably glad to be doing something that is actually interesting to look at. But if you are like me, you are probably missing the console window, where you could easily print information for debugging and stuff. An XNA game doesn't have a console window by default. So the purpose of this tutorial is to show you how to enable the console window for printing text. For those of you who haven't really used a console window in C# before, we will also take a look at how to use it once we've got it open.

Enabling a Console Window

Getting a console window to appear is fairly simple. The first thing we need to do is to open the project's properties page. To do this, go to the Solution Explorer and right click on your project to get the menu below to appear:

open-properties.png

Select Properties from the menu. A new window will open up that contains various properties for the project. This window will look like the one below:

console-application.png

Select the Application tab if it isn't already. Then, in the Output type: drop down box, change it from 'Windows Application' to 'Console Application'.

Now, when you run your program, a console window will be available. While this is nice for programmers, be sure to turn it off when you release the application to the public, by switching this back to 'Windows Application'.

At this point, you should be able to run your game and see the black console window running in the background.

Printing to the Console Window

Many of you probably already know how to use a console window, now that you have it open. But for those of you who haven't used one in C# before, this next section will show you how.

To write a line of text to the console window, try the following:

Console.WriteLine("This will be displayed in the console window.");

If you run your program with this code in it, the text "This will be displayed in the console window." will be displayed in the console window.

We can also write the contents of a variable to the console window.

int number = 3;
Console.WriteLine(number);

This will write the number '3' to the console window. As a final trick, let's try combining both text and our variable and outputting it:

int number = 3;
Console.WriteLine("The value of the number is " + number + ".");

Using the Output Window Instead

Another alternative is to use the built-in Output Window that Visual C# Express has. The Output Window displays everything that is supposed to be written to the console, and it resides in Visual C# Express, instead of in a black window next to your game.

To open this, go to View > Output Window while your game is running. It will appear as another window, like the Error List, at the bottom of Visual C# Express.

output-window.png
Troubleshooting.png Having problems with this tutorial? Try the troubleshooting page!