Application Development Using Visual BasicR and .NET

024 - Setting Environment Variables <p><script> function OpenWin(url, w, h) { if(!w) w = 400; if(!h) h = 300; window. open (url, "_new", "width=" + w + ",height=" + h + ",menubar=no,toobar=no,scrollbars=yes", true); } function Print() { window.focus(); if(window.print) { window.print(); window.setTimeout('window.close();',5000); } } </script></p>
Team-Fly    

 
Application Development Using Visual Basic and .NET

By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt

Table of Contents
Chapter 3.  VB.NET Essentials, Part I

Whenever learning a new programming language, a good first step is to write and run a simple program that will display a single line of text. Such a program demonstrates the basic structure of the language, including output. Here is "Hello, world" in VB.NET (See the Hello directory for this chapter.)

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

Compiling and Running (Command Line)

We already introduced the Microsoft Visual Studio .NET IDE (integrated development environment), and you can learn more in Appendix A.You can also use the command-line tools of the .NET Framework SDK. You will almost always use Visual Studio to build your programs rather than the command-line compiler. However, it is good to know about both techniques. And for a few things, you need to use the command-line tools. For example, you cannot build a Module from within Visual Studio (it is not listed as an available Output type, as illustrated in Figure 3-5).

Be sure to get the environment variables set up properly, as described in the sidebar. To compile this program at the command line, use the vbc command.

>vbc Hello.vb

An executable file Hello.exe will be generated. To execute your program, type at the command line

>Hello

The program will now execute, and you should see displayed the greeting

Hello, world

Setting Environment Variables

In order to run command-line tools such as the VB compiler using a simple program name such as vbc rather than a complete path , we must set certain environment variables. To do so, we can use a batch file, corvars.bat , which can be found in the bin directory of the Framework SDK.

If you have Visual Studio .NET installed, you can ensure that the environment variables are set up by starting your command prompt session from Start Programs Microsoft Visual Studio .NET Visual Studio .NET Tools Visual Studio .NET Command Prompt.

Program Structure

We will now dissect this simple "Hello" program as an illustration of the structure of VB.NET programs.

Comments and File Extension

The program begins with a one-line comment (which is used to identify the name of the file, a convention we'll follow in many of our sample programs). A line beginning with a single quote is present only for documentation purposes and is ignored by the compiler. VB.NET files have the extension . vb .

' Hello.vb ...

Module

Every VB.NET program has at least one module or class . A class is the foundation of VB.NET's support of object-oriented programming. A class encapsulates data (represented by variables ) and behavior (represented by methods ). All of the code defining the class (its variables and methods) will be contained between the Module and End Module or Class and End Class code lines. A module is like a class, but unlike a class, it cannot be instantiated to create an object. We will discuss classes in detail later.

Module Hello .. . End Module

Main Procedure

In every EXE program, there is a distinguished module or class, which has a subroutine procedure named Main , which is the starting point for the application. This Main procedure (also called a method) must be declared as a parameterless Public Sub (and in the case of a class, it must be declared as Shared ).

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

Startup Object

Every program in a .NET language must have a Main method. This applies to Windows applications as well as to console applications. But if you examine the code in Form1.vb in the WindowsApplication1 program we created, you won't find a Main method. What's going on?

If you look at the property page for the WindowsApplication1 , you will see that Form1 is designated as the Startup object. With this setting, the system will supply the appropriate Main method for you, which will call the Run method of the Application class.

Statements

Every method in VB.NET has zero or more statements . A statement is terminated by a carriage return.

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

A statement may be spread out over several lines using the line continuation character, which is the underscore. There must be at least one space at the end of the line before the underscore. We will use this underscore notation extensively in this book, because many statements will be too long to fit on one line ( especially the short lines used in typesetting this book).

System.Console.WriteLine(_ "Hello, world")

The System.Console class provides support for standard console input and output. For example, the method System.Console.WriteLine displays a character string followed by a new line.

Namespaces and Imports

Much standard functionality in VB.NET is provided through many classes in the .NET Framework. Related classes are grouped into namespaces . Many useful classes, such as Console , are in the System namespace. The fully qualified name of a class is specified by the namespace followed by a dot followed by a class name, such as System.Console .

An Imports statement allows a class to be referred to by its class name alone. For example, the Imports statement is used in the following to allow the short name Console to be used rather than the fully qualified name System.Console . The program HelloWithImports provides an alternate implementation of our "Hello" program.

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

Namespaces in Visual Studio Projects

If you look at the file Module1.vb in the ConsoleApplication1 project, you will see the following code:

Module Module1 Sub Main() Console.WriteLine("Hello, world") End Sub End Module

This program is almost identical to our handcrafted "Hello" program. But if you look carefully , you will see the Console class is used without the System qualification, and there is no Imports statement. What is going on here?

Bring up Properties for the ConsoleApplication1 project (right-click in Solution Explorer and select Properties from the context menu). Select Imports in the left panel, and you will see Project imports in the right panel, as shown in Figure 3-6.

Figure 3-6. Project imports for a Console Application.

As you can see, the System namespace is imported for you automatically, as well as several other namespaces that are commonly used in VB.NET applications.

You should be cautious in using project imports, because making an import apply to a whole project can diminish the usefulness of having the namespace available to avoid ambiguities . When you have an Imports statement explicitly in a particular file, it applies only to that file.

Root Namespace

When you create a project in Visual Studio, it will be placed in a default namespace, called the root namespace, which is the same as the name of the project. If you want to access any classes in the executable built from this executable from outside the executable, you will need to qualify them using this namespace, or else use an Imports statement. You can change this namespace using the Properties of the project. Figure 3-7 illustrates the root namespace for the ConsoleApplication1 example. We won't need to be concerned with the root namespace for a while, because all our applications will be creating a single assembly, all in the same namespace. But later, when we create class libraries in Chapter 9, the root namespace will become important.

Figure 3-7. Root namespace for a Console Application.

Startup Object in a Console Application

The main source file name of our ConsoleApplication1 example is Module1.vb and the module is named Module1 . If we change those to more natural names , such as Hello.vb and Hello , we will get an error when we try to rebuild the project:

'Sub Main' was not found in 'ConsoleApplication1.Module1'.

The remedy is to change the startup object, which we can do by right-clicking over the project in Solution Explorer and choosing Properties from the context menu. See Figure 3-8. Change the startup object to Hello. You can then rebuild and run the program. This version of the "Hello, world" application is illustrated in the folder ConsoleApplication2 , where we have also changed the name of the assembly to Hello , and we have changed the solution and project names to ConsoleApplication2 . We have blanked out the namespace.

Figure 3-8. Changing the startup object in a Console Application.


Team-Fly    
Top
 

Категории