Previous section   Next section

2.1 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 program uses many features of the language that will be discussed in coming chapters, including statements, methods, objects, strings, inheritance, blocks, libraries, and polymorphism.

This chapter provides a whirlwind tour of a number of these concepts. I'll then spend the rest of the book expanding on these areas and showing how they can be applied to create .NET applications.

Each program consists of a series of statements, which are instructions to the complier. In VB.NET, as in previous versions of Visual Basic, every statement ends with a carriage return/linefeed; you create one by pressing the Enter key.

The first line in Example 2-1 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 this line:

End Module

Within the HelloWorld module you define a method, or programming routine, called Main( ). 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 to signal the beginning of the subroutine and the End Sub line to conclude 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, as in previous versions of VB, 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 (subroutine).

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 5.) Within Main( ) is a single line of code:

System.Console.WriteLine("Hello World")

This line of code calls a method (WriteLine) on an object (Console) within the System namespace. Let's take that apart, piece by piece.

2.1.1 Classes and Objects

A class defines a type. An object is an individual instance of a class. In the preceding example, Console is an object that represents your screen. The Console class defines what it means to be a Console object; it defines what Console objects can do and what information Console objects can store (these characteristics are known as the object's state).

Similarly, the class Button defines what it means to be a button. The Button class defines that Button objects can be clicked, drawn, etc., and it defines what information they can store (e.g., the text label on the button). The individual buttons on a form are instances of the Button class; in other words, the individual buttons are Button objects. Each object has its own state. For example, each Button object has its own text label. One button might read "OK", while another reads "Push Me". Classes and objects are described in detail in Chapter 5.

2.1.2 Namespaces

In the HelloWorld program, the Console class is defined within the System namespace. Each VB.NET class must have a unique name. Console is only one of a tremendous number of useful types that are part of the .NET Framework Class Library. Each class has a name, and thus the FCL contains thousands of names, such as ArrayList, FileDialog, DataException, EventArgs, and so on. Names and more names; hundreds, thousands, even tens of thousands of names.

This presents a problem. No developer can possibly memorize all the names that the .NET Framework uses, and sooner or later you are likely to create an object and give it a name that has already been used. What will happen if you develop your own Hashtable class, only to discover that it conflicts with the Hashtable class that .NET provides?

You certainly could rename your Hashtable class mySpecialHashtable, for example, but that is a losing battle. New Hashtable types are likely to be developed, and distinguishing between their type names and yours would be a nightmare.

The solution to this problem is provided by the namespace. A namespace restricts a name's scope, making it meaningful only within the defined namespace. Namespaces can help you organize and compartmentalize your types. System is one of the default namespaces VB.NET provides.

However, when you write a complex Visual Basic .NET program, you might want to create your own namespace hierarchy. There is no limit to how deep this hierarchy can be. The goal of namespaces is to help you divide and conquer the complexity of your object hierarchy.

For instance, assume that I tell you that Jim is an engineer. The word "engineer" is used for many things in English and can cause confusion. Does Jim design buildings? Write software? Run a train?

In English I might clarify by saying "he's a scientist" or "he's a train engineer." A Visual Basic .NET programmer could tell you that Jim is a Science.Engineer rather than a Train.Engineer. The namespace (in this case, Science or Train) restricts the scope of the word that follows. It creates a "space" in which that name is meaningful.

Further, it might happen that Jim is not just any kind of Science.Engineer. Perhaps Jim graduated from MIT with a degree in software engineering, not civil engineering (are civil engineers especially polite?). Thus, the object that is Jim might be defined more specifically as a Science.Software.Engineer. This classification implies that the namespace Software is meaningful within the namespace Science, and that Engineer in this context is meaningful within the namespace Software. If later you learn that Charlotte is a Transportation.Train.Engineer, you will not be confused as to what kind of engineer she is. The two uses of Engineer can coexist, each within its own namespace.

Similarly, if it turns out that .NET has a Hashtable class within its System.Collections namespace, and that I have also created a Hashtable class within a ProgVBNET.DataStructures namespace, there is no conflict because each exists in its own namespace.

2.1.3 The WriteLine( ) Method

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, separated by the dot operator, as follows:

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 about that namespace. You do so by adding an Imports declaration to the top 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 Dot Operator (.)

In Example 2-1, the dot operator (.) is used both to access a method (and data) in a class (in this case, the method WriteLine( )), and to restrict the class name to a specific namespace (in this case, to locate Console within the System namespace). This works well because in both cases we are "drilling down" to find the exact thing we want. The top level is the System namespace (which contains all the System objects that the Framework provides); the Console type exists within that namespace, and the WriteLine( ) method is a member function of the Console type.

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

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

Visual Studio .NET automatically (and invisibly) adds the Imports System statement for you (as well as several other commonly used namespaces). Thus, if you write this Hello World program in Visual Studio .NET, you do not need to explicitly add the Imports System statement. However, keep in mind that you may need to explicitly import other namespaces for more complicated programs.

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

2.1.4 Comments

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. 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.


  Previous section   Next section
Top