Creating a Console Application

for RuBoard

As our first exercise in using Visual Studio, we will create a simple console application. Our program Bytes will attempt to calculate how many bytes there are in a kilobyte, a megabyte, a gigabyte, and a terabyte. If you want to follow along on your PC as you read, you can use the Demos directory for this chapter. The first version is in Bytes\Step1 . A final version can be found in Bytes\Step3 .

Creating a C# Project

  1. From Visual Studio main menu choose File New Project.... This will bring up the New Project dialog.

  2. For Project Types choose "Visual C# Projects" and for Templates choose "Empty Project."

  3. Click the Browse button, navigate to Demos, and click Open .

  4. In the Name field, type Bytes . See Figure A-6. Click OK.

Figure A-6. Creating an empty C# project.

graphics/apafig06.gif

Adding a C# File

At this point you will have an empty C# project. We are now going to add a file Bytes.cs, which contains the text of our program.

  1. In Solution Explorer right-click over Bytes and choose Add Add New Item.... This will bring up the Add New Item dialog.

  2. For Categories choose "Local Project Items" and for Templates choose "Code File."

  3. For Name type Bytes.cs . See Figure A-7. Click Open.

Figure A-7. Adding an empty C# file to a C# project.

graphics/apafig07.gif

Using the Visual Studio Text Editor

In the Solution Explorer double-click on Bytes.cs . This will open up the empty file Bytes.cs in the Visual Studio text editor. Type in the following program, and notice things like color syntax highlighting to indicate reserved words as you type.

 // Bytes.cs  using System;  public class Bytes   {      public static int Main(string[] args)      {         int bytes = 1024;         Console.WriteLine("kilo = {0}", bytes);         bytes = bytes * 1024;         Console.WriteLine("mega = {0}", bytes);         bytes = bytes * 1024;         Console.WriteLine("giga = {0}", bytes);         bytes = bytes * 1024;         Console.WriteLine("tera = {0}", bytes);         return 0;    }  } 

Besides the color syntax highlighting, other features include automatic indenting. All in all, you should find the Visual Studio editor friendly and easy to use.

Building the Project

You can build the project by using one of the following:

  • Menu Build Build

  • Toolbar graphics/icon07.gif

  • Keyboard shortcut Ctrl + Shift + B

Running the Program

You can run the program by using one of the following:

  • Menu Debug Start Without Debugging

  • Toolbar graphics/icon08.gif

  • Keyboard shortcut Ctrl + F5

You will see the following output in a console window that opens up:

 kilo = 1024  mega = 1048576  giga = 1073741824  tera = 0  Press any key to continue 

We will investigate the reason for the strange output later. If you press any key, as indicated, the console window will close.

Running the Program in the Debugger

You can run the program in the debugger by using one of the following:

  • Menu Debug Start

  • Toolbar graphics/icon16.gif

  • Keyboard shortcut F5

A console window will briefly open up and then immediately close. If you want the window to stay open, you must explicitly program for it, for example, by asking for input. You can set a breakpoint to stop execution before the program exits. We will outline features of the debugger later in the chapter.

for RuBoard


Application Development Using C# and .NET
Application Development Using C# and .NET
ISBN: 013093383X
EAN: 2147483647
Year: 2001
Pages: 158

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net