There are two obvious ways to enter, compile, and run the programs in this book. You can enter the text into a text editor like Notepad and then use the command-line compiler, or you can use the Visual Studio .NET Integrated Development Environment (IDE) to write the code and then request that the IDE call the compiler for you.
The job of the compiler is to turn your source code into a working program. It turns out to be just slightly more complicated than that because .NET uses an intermediate language called Microsoft Intermediate Language (MSIL, sometimes abbreviated to IL). The compiler reads your source code and produces IL. The .NET Just In Time (JIT) compiler then reads your IL code and produces an executable application in memory.
You can enter source code like the "Hello World" program from Example 2-1 in any text editor, such as Notepad. You then save the code in a text file. For instance, you might name the file containing the "Hello World" program HelloWorld.vb.
You can then compile the source code by opening the Visual Studio .NET Command Prompt. In order to ensure that your compiler environment variables are set properly (so that your compiler will work properly) you will want to open a special DOS box provided in the .NET SDK. After installing the SDK, you will typically find this program at:
"C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\vsvars32.bat"
I recommend that you save a shortcut to this on your desktop, but you can also open the Visual Studio .NET Command Prompt by using the menu sequence
Start -> Programs -> Microsoft Visual Studio .NET 2003 -> Visual Studio .NET Tools -> Visual Studio .NET Command Prompt
On the command line, enter the name of the VB.NET compiler program, vbc, passing in the source, as in the following:
vbc HelloWorld.vb
The Microsoft VB.NET compiler will compile your code; when you display the directory you'll find that the compiler has produced an executable file called HelloWorld.exe. Type HelloWorld at the command prompt, and your program will execute, as shown in Figure 2-1.
If you prefer, you can compile the program in debug mode:
vbc /debug HelloWorld.vb
The /debug command-line switch inserts symbols in the code, which helps when you run your program in a debugger, introduced later in this chapter.
Just In Time CompilationCompiling HelloWorld.vb using vbc creates an executable (.exe) file. Keep in mind, however, that the .exe file contains op-codes written in Microsoft Intermediate Language (MSIL), which is introduced in Chapter 1. Interestingly, if you wrote this application in C# or any other language compliant with the .NET Common Language Specification (CLS), compiling it would produce the same MSIL. By design Intermediate Language code created from different languages is virtually indistinguishable, which is the point of having a common language specification in the first place. In addition to producing the IL code (which is similar in spirit to Java's byte- code), the compiler creates a read-only segment of the .exe file in which it inserts a standard Win32 executable header. The compiler designates an entry point within the read-only segment; the operating system loader jumps to that entry point when you run the program, just as it would for any Windows program. The operating system cannot execute the IL code, however, and that entry point does nothing but jump to the .NET Just In Time compiler (also introduced in Chapter 1). The JIT produces native CPU instructions, as you might find in a normal .exe. The key feature of a JIT compiler, however, is that functions are compiled only as they are used, just in time for execution. |
Rather than writing your program in Notepad and compiling it at the command line, you can write and compile your program using the Visual Studio .NET Integrated Development Environment. The IDE provides enormous advantages. These include automatic indentation, IntelliSense word completion, color coding, and integration with the help files. Most important, the IDE includes a powerful debugger and a wealth of other tools.
I strongly recommend that you spend some time exploring the Visual Studio .NET Integrated Development Environment. This is your principal tool as a .NET developer, and you want to learn to use it well. Time invested up front in getting comfortable with Visual Studio .NET will pay for itself many times over. The following pages provide only a short overview of some of the IDE's most basic capabilities. To get the most out of Visual Studio .NET, spend the time to explore and read the documentation. It is a very powerful tool that will serve you well.
To create the "Hello World" program in the IDE, first open Visual Studio .NET. You can use the Visual Studio .NET desktop icon or select Visual Studio .NET from your Start menu, using the following sequence:
Start -> Programs -> Microsoft Visual Studio .NET 2003
Then choose File->New Project from the menu toolbar. This will invoke the New Project window. Figure 2-2 shows the New Project window.
To open a project to contain your application, select Visual Basic .NET Projects in the Project Type window and select Console Application in the Templates window. You can now enter a name for the project (e.g., HelloWorld) and select a directory in which to store your files. Then click OK, and a new window will appear in which you can enter the code, as shown in Figure 2-3.
Visual Studio .NET creates a module named Module1, which you are free to rename. When you rename the module, be sure to also change the name of the default file (Module1.vb). To reproduce Example 2-1, for instance, you change the name of Module1 to HelloWorld, and rename the Module1.vb file (listed in the Solution Explorer window) to HelloWorld.vb.
Finally, Visual Studio .NET creates a program skeleton. Replace the code provided by Visual Studio .NET with the code shown in Example 2-1.
Once you've entered the code you want, you are ready to compile and run the program. There are many ways to compile and run the "Hello World" program from within Visual Studio .NET. Typically you can accomplish every task by choosing commands from the Visual Studio .NET menu toolbar, by using buttons, or, in many cases, by using key-combination shortcuts.
For example, you can test your program within the debugger by pressing F5 (or by choosing Debug->Start), or you can run outside the debugger by pressing Ctrl-F5 (or by choosing Debug->Start Without Debugging) or by clicking the Start button, as shown in Figure 2-4. In either case, this will build your program and run it.
You can build your program without running it (e.g., just to check for compile errors) by pressing Ctrl-Shift-B, or by choosing Build->Build Solution or by clicking the Build button as shown in Figure 2-5. Updates to your source code may or may not be saved each time you build (whether or not you run) depending on how your options are set (Tools->Options).
|
Top |