Section 2.2. Your First Program: Hello World

   

2.2 Your First Program: Hello World

In this chapter, you will create a very simple application that does nothing more than display the words "Hello World" to your monitor. This basic console application is the traditional first program for learning any new language; it demonstrates some of the basic elements of a VB.NET program.

Once you write your "Hello World" program and compile it, this chapter will provide a line-by-line analysis of the source code. This analysis gives something of a preview of the language, the fundamentals of which are described much more fully in Chapter 5.

As explained earlier, you can create VB.NET programs with any text editor. You can, for example, create each of the three programs shown previously (in Figure 2-1, Figure 2-2, and Figure 2-3) with Notepad. To demonstrate that this is possible, you'll write your very first VB.NET program using Notepad.

Begin by opening Notepad and typing in the program exactly as shown in Example 2-1.

Example 2-1. Hello World in Notepad
 Module HelloWorld    ' every console app starts with Main    Sub Main( )       System.Console.WriteLine("Hello world!")    End Sub End Module 

That is the entire program. Save it to your disk as a file called helloworld.vb .

We'll examine this program in some detail in just a moment. First, however, it must be compiled.

2.2.1 The Compiler

Once you save your program to disk, you must compile the code to create your application. Compiling your source code means running a compiler and passing in the source code file. You run the compiler by opening a command prompt (DOS box) and entering the program name vbc . Then you "pass in" your source code file by entering the filename on the command line, as in the following:

 vbc helloworld.vb 

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.

Microsoft provides a command window (through Visual Studio .NET) with the correct environment variables set. Open a command window by selecting the following menu items in this order:

 Start -> Programs -> Microsoft Visual Studio .NET  -> Visual Studio.NET Tools -> Visual Studio .NET Command Prompt 

Then navigate to the directory in which you created your code file and enter the following command:

 vbc helloworld.vb 

The Microsoft VB.NET compiler compiles your code; when you display the directory you'll find 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-4.

Figure 2-4. Compiling and running Hello World
figs/lvbn_0204.gif

Presto! You are a VB.NET programmer. That's it, close the book, you've done it. Okay, don't close the book; there are details to examine, but take a moment to congratulate yourself. Have a cookie.

Granted, the program you created is one of the simplest VB.NET programs imaginable, but it is a complete VB.NET program, and it can be used to examine many of the elements common to VB.NET programs.

   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

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