Section 2.3. Examining Your First Program

   

2.3 Examining Your First Program

The single greatest challenge when learning to program is that you must learn everything before you can learn anything. Even this simple "Hello World" program uses many features of the language that will be discussed in coming chapters, including classes, namespaces, statements, static methods , objects, strings, inheritance, blocks, libraries, and even something called polymorphism!

It is as if you were learning to drive a car. You must learn to steer, accelerate, brake, and understand the flow of traffic. Right now we're going to get you out on the highway and just let you steer for a while. Over time you'll learn how to speed up and slow down. Along the way you'll learn to set the radio and adjust the heat so that you'll be more comfortable. In no time you'll be driving, and then won't your parents begin to worry.

2.3.1 Line-by-Line Analysis

Hang on tight; we're going to zip through this quickly and come back to the details in subsequent chapters. The first line in the program defines a programming unit known as a module . In this case, the module is named HelloWorld:

 Module HelloWorld 

You begin each module definition using the Module keyword, as in the preceding code line. Likewise, you end each module definition with the line:

 End Module 

Within the module definition, you write other programming constructs. For instance, you might define what is called an object . An object is an individual instance of a thing. Every object belongs to a more general category known as a class . While a class defines a type, each instance of that type is an object (much as Car defines a type of vehicle, and your aging rust-bucket is an individual instance of Car).

In VB.NET there are thousands of classes. Classes are used to define Windows controls ( buttons , list boxes, etc.), as well as types of things ( employees , students, telephones, etc.) in the program you are writing. Some classes you create yourself; some you obtain from the .NET Framework. Each class must be named. Classes are the core of VB.NET and object-oriented programming.

For now, keep in mind that modules are actually related to classes. Technically, modules are a holdover from the previous generation of the VB language, VB6. In order to adapt VB6 for object-oriented programming, VB.NET converts modules to classes for you. You'll learn about classes in Chapter 3 and about modules and classes in Chapter 8.

Within the HelloWorld module, you define a method called Main( ). A method is a small block of code that performs an action. The Main( ) method is the "entry point" for every VB.NET console application; it is where your program begins. Within the HelloWorld module, the Main( ) method is defined from lines 3 through 5. Notice the Sub keyword signals the beginning of the subroutine and the End Sub line concludes the method:

 Sub Main( )     System.Console.WriteLine("Hello world!") End Sub 

Typically, one method calls another. The called method will do work, and it can return a value to the calling method. In VB.NET, methods come in two flavors: a method that returns a value is called a function ; a method that does not return a value is called a sub (for subroutine). (Function and subroutine are old, non-object-oriented terms for these kinds of methods.) You'll see how methods call one another and return values in Chapter 9.

Main( ) is called by the operating system (when the program is invoked). Every method name is followed by opening and closing parentheses:

 Sub Main  ( )  

As the parentheses imply, it is possible to pass values into a method so that the method can manipulate or use those values. These values are called parameters or arguments to the method. In this case, Main( ) has no arguments. (Method arguments are covered in Chapter 9.)

Within Main( ) is a single line of code:

 System.Console.WriteLine("Hello world!") 

WriteLine( ) is a method that is called by the Main( ) method; more about WriteLine( ) shortly. The Console is an object that represents your screen. In this case, each Console object belongs to the Console class. In VB.NET, classes can exist within a more comprehensive grouping known as a namespace .

In the HelloWorld program, the Console class is defined within the System namespace. The Console class has a method, WriteLine( ), that displays a line of text to the screen. The complete identification for the WriteLine( ) method includes the class and namespace to which it belongs:

 System.Console.WriteLine("Hello world!") 

The WriteLine( ) method declares a single parameter, the text string you want to display. When you pass in a string to the method, the string is an argument. In our sample program, the string "Hello world!" corresponds to the parameter the method expects; thus, the string is displayed to the screen.

If you will be using many objects from the same namespace, you can save typing by telling the compiler that many of the objects you'll be referring to are in that namespace. You do so by adding an Imports declaration to the beginning of your program:

 Imports System 

Once you add this line, you can use the Console class name without explicitly identifying its namespace (System). Thus, if you add the preceding Imports declaration, you can rewrite the contents of Main( ) as follows :

 Console.WriteLine("Hello world!") 

The compiler will check the namespace you identified (System), and it will find the Console class defined there.

Since the method (or sub) is defined within the module, you do not close the module until you have closed the method. Thus, the program ends with the sequence:

 End Sub End Module 

This discussion has omitted a single line in our program. Just before the start of the Main( ) method appears a comment (here in bold):

 '  every console app starts with Main  Sub Main( )     System.Console.WriteLine("Hello world!") 

A comment is just a note to yourself. You insert comments to make the code more readable to programmers. You can place comments anywhere in your program that you think the explanation will be helpful; they have no effect on the running program.

In VB.NET, comments begin with a single quotation mark. The quote indicates that everything to the right on the same line is a comment and will be ignored by the VB.NET compiler.

Whew! That was a lot to take in all at once! Don't panic; all of the concepts introduced here are explained in detail in later chapters.

   


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