| 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 -
From the Visual Studio main menu, choose File New Project. This will bring up the New Project dialog. -
For Project Types, choose Visual Basic Projects, and for Templates, choose Empty Project. -
Click the Browse button, navigate to Demos , and click Open. See Figure A-6. Figure A-6. Creating an empty VB.NET project. -
In the Name field, type Bytes . -
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. -
In the Solution Explorer, right-click over Bytes , and choose Add Add New Item. This will bring up the Add New Item dialog. -
For Categories, choose Local Project Items. -
For Templates, choose Module. -
For Name, type Bytes.vb . See Figure A-7. Figure A-7. Adding a VB.NET module to a VB.NET project. -
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: Running the Program You can run the program using one of the following: 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. 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 -
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. |