Creating a Console Application

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Appendix A.  Visual Studio .NET


graphics/codeexample.gif

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 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\Version1 . A final version can be found in Bytes\Version3 .

Creating a VB.NET Project

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

  2. For Project Types, choose Visual Basic Projects, and for Templates, choose Empty Project.

  3. Click the Browse button, navigate to Demos , and click Open. See Figure A-6.

    Figure A-6. Creating an empty VB.NET project.

    graphics/afig06.jpg

  4. In the Name field, type Bytes .

  5. Click OK.

Adding a VB.NET Module

At this point, you have an empty VB.NET project. We will now add a file called Bytes.vb , which will contain the source code for our program.

  1. In the 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.

  3. For Templates, choose Module.

  4. For Name, type Bytes.vb . See Figure A-7.

    Figure A-7. Adding a VB.NET module to a VB.NET project.

    graphics/afig07.jpg

  5. Click Open.

Using the Visual Studio Text Editor

In the Solution Explorer, double-click on Bytes.vb . This will open the file in the Visual Studio text editor. Notice that a skeleton version of a module already exists because you selected Module as the template for the file. Type the following code in Bytes.vb . You should notice that the editor uses color -coded syntax highlighting as you type.

 ' Bytes.vb - Version 1 Imports System Module Bytes    Sub Main()       Dim bytes As Integer = 1024       Console.WriteLine("1 kilobyte = {0} bytes", bytes)       bytes = bytes * 1024       Console.WriteLine("1 megabyte = {0} bytes", bytes)       bytes = bytes * 1024       Console.WriteLine("1 gigabyte = {0} bytes", bytes)    End Sub End Module 

Besides the color syntax highlighting, other features include automatic indenting and automatically adding End statements to various programming constructs. All in all, you should find the Visual Studio editor friendly and easy to use.

Building the Project

You can build the project using one of the following:

  • Menu Build Build

  • Toolbar graphics/buildtoolbar_icon.jpg

  • Keyboard shortcut Ctrl + Shift + B

Running the Program

You can run the program using one of the following:

  • Menu Debug Start Without Debugging

  • Toolbar graphics/debugtoolbar_icon.jpg

  • Keyboard shortcut Ctrl + F5

You won't see any output. This is because the default project type for an "empty project" is a Windows application. To change it, you must right-click on the project name ( Bytes ) in the Solution Explorer window and choose Properties. See Figure A-8. In the Output type dropdown, select Console Application. Using the Project Properties dialog, you can also configure the Startup Object of the applications.

Figure A-8. Changing project properties.

graphics/afig08.jpg

Now rebuild your application, and try to run it again. You will see the following output in a console window:

 1 kilobyte = 1024 bytes 1 megabyte = 1048576 bytes 1 gigabyte = 1073741824 bytes Press any key to continue 

If you press any key, as indicated, the console window will close.

Defining the Startup Object

In VB.NET, you must define the startup object. You do this via the project Properties dialog shown in Figure A-8. Depending on how the project was built and whether you changed the name of the module after it was generated, you may need to set the startup object to Sub Main . The compiler will generate a syntax message if it is unable to find Sub Main .

Running the Program in the Debugger

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

  • Menu Debug Start

  • Toolbar graphics/start_icon.jpg

  • 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. Add the following two lines to your program right above the End Sub statement.

 Console.WriteLine("Press enter to continue...") Console.ReadLine() 

When you run your program, the output will stay on the screen until you press the Enter key. You can also set a breakpoint to stop the execution before the program exits. We will outline features of the debugger later in the appendix.


Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

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