An Example Visual Basic .NET Program - O'Reilly

hello, world

This is the world’s favorite programming example, translated to Visual Basic .NET:

Imports System Public Module Hello Public Sub Main( ) Console.WriteLine("hello, world") End Sub End Module

This version of hello, world is a console application -- it displays its output in a Windows command-prompt window. To compile this program, enter it using any text editor, such as Windows’s Notepad, save it in a file whose name ends with .vb, such as Hello.vb, and compile it from the Windows command line with this command:

vbc Hello.vb

The command vbc invokes the Visual Basic .NET command-line compiler, which ships with the .NET Framework SDK, and instructs it to compile the file named in the command-line argument. Compiling Hello.vb generates the file Hello.exe. After compiling, type Hello at the command line to run your program. Figure 1-1 shows the results of compiling and running this program.

Compiling and running hello, world

Figure 1-1. Compiling and running hello, world

If you’re accustomed to programming in Visual Basic 6, you can see even from this little program that Visual Basic has changed dramatically. Here’s a breakdown of what’s happening in this code.

The first line:

Imports System

indicates that the program may use one or more types defined in the System namespace . (Types are grouped into namespaces to help avoid name collisions and to group related types together.) Specifically, the hello, world program uses the Console class, which is defined in the System namespace. The Imports statement is merely a convenience. It is not needed if the developer is willing to qualify type names with their namespace names. For example, the hello, world program could have been written this way:

Public Module Hello Public Sub Main( ) System.Console.WriteLine("hello, world") End Sub End Module

However, it is customary to use the Imports statement to reduce keystrokes and visual clutter.

An important namespace for Visual Basic developers is Microsoft.VisualBasic. The types in this namespace expose members that form Visual Basic’s intrinsic functions and subroutines. For example, the Visual Basic Trim function is a member of the Microsoft.VisualBasic.Strings class, while the MsgBox function is a member of the Microsoft.VisualBasic.Interaction class. In addition, Visual Basic’s intrinsic constants come from enumerations within this namespace. Much of the functionality available in this namespace, however, is also duplicated within the .NET Framework’s Base Class Library. Developers who are not familiar with Visual Basic 6 will likely choose to ignore this namespace, favoring the functionality provided by the .NET Framework. The .NET Framework is introduced later in this chapter and is explained in detail in Chapter 3.

Next, consider this line:

Public Module Hello

This line begins the declaration of a standard module named Hello. The standard-module declaration ends with this line:

End Module

In Visual Basic 6, various program objects were defined by placing source code in files having various filename extensions. For example, code that defined classes was placed in .cls files, code that defined standard modules was placed in .bas files, and so on. In Visual Basic .NET, all source files have .vb filename extensions, and program objects are defined with explicit syntax. For example, classes are defined with the Class...End Class construct, and standard modules are defined with the Module...End Module construct. Any particular .vb file can contain as many of these declarations as desired.

The purpose of standard modules in Visual Basic 6 was to hold code that was outside of any class definition. For example, global constants, global variables, and procedure libraries were often placed in standard modules. Standard modules in Visual Basic .NET serve a similar purpose and can be used in much the same way. However, in Visual Basic .NET they define datatypes that cannot be instantiated and whose members are all static. This will be discussed in more detail in Chapter 2.

The next line in the example begins the definition of a subroutine named Main:

Public Sub Main( )

It ends with:

End Sub

This syntax is similar to Visual Basic 6. The Sub statement begins the definition of a subroutine -- a method that has no return value.

The Main subroutine is the entry point for the application. When the Visual Basic .NET compiler is invoked, it looks for a subroutine named Main in one of the classes or standard modules exposed by the application. If Main is declared in a class rather than in a standard module, the subroutine must be declared with the Shared modifier. This modifier indicates that the class does not need to be instantiated for the subroutine to be invoked. In either case, the Main subroutine must be Public. An example of enclosing the Main subroutine in a class rather than in a standard module is given at the end of this section.

If no Main subroutine is found, or if more than one is found, a compiler error is generated. The command-line compiler has a switch (/main: location) that allows you to specify which class or standard module contains the Main subroutine that is to be used, in the case that there is more than one.

Lastly, there’s the line that does the work:

Console.WriteLine("hello, world")

This code invokes the Console class’s WriteLine method, which outputs the argument to the console. The WriteLine method is defined as a shared (also known as a static) method. Shared methods don’t require an object instance in order to be invoked; nonshared methods do. Shared methods are invoked by qualifying them with their class name (in this case, Console).

Here is a program that uses a class instead of a standard module to house its Main subroutine. Note that Main is declared with the Shared modifier. It is compiled and run in the same way as the standard module example, and it produces the same output. There is no technical reason to choose one implementation over the other.

Imports System Public Class Hello Public Shared Sub Main( ) Console.WriteLine("hello, world") End Sub End Class

Từ khóa » Visual Basic Dotnet Sample Projects