2.1 Creating and Compiling the Component

only for RuBoard

Example 2-1 contains the listing for our "Hello, world" component. It contains a single class named Hello with a single method named Write . Save the listing to a file named hello.vb . The rest of the chapter will use this listing as a foundation of discussion.

All Visual Basic source code should be saved to files with a .vb extension. One file can contain one class or several classes. How you organize the code is up to you.

Example 2-1. The "Hello, world" component
 Option Strict On     Imports System     Namespace Greeting     Public Class Hello     Public Sub Write(ByVal value As String)         Console.WriteLine("Hello, {0}!", value)     End Sub End Class     End Namespace 

The Visual Basic .NET command-line compiler is a program called vbc.exe that should be in your path once the .NET Framework is installed. All examples in this book assume that the example code exists in the root directory of your hard drive. This assumption is made to improve readability. If the code is not in your hard drive's root directory, you need to specify a fully qualified pathname to the compiled file or compile from the directory where the source code is located. With this in mind, you should be able to compile Example 2-1 to a dynamic link library (DLL) as follows :

 C:\>vbc /t:library hello.vb 

The /t : option is short for target , which can be one of the following values:

exe

A console application. If the /t switch is omitted, this is the default value.

winexe

A Windows executable.

library

A DLL.

module

A module. This value is similar to a .lib file in C++. It contains objects but is not an executable.

As a default, the compiler gives the output file the same name as the file being compiled. Here, a file named hello.dll is produced.

All examples in this book assume that Option Strict is turned on. Option Strict prevents the VB compiler from making implicit narrowing type conversions, which is often a source of errors. Implicit narrowing type conversion involves using one type where another type with a smaller range is expected. For example, the following code is illegal when Option Strict is turned on:

 Dim x As Short = 5 Dim b As Byte = x 

The code is illegal because x is a Short , a type whose range is -32,768 through 32,767 . The code here assigns the value of this type to a Byte , an unsigned numeric type whose range is to 255 . Since Short values from -32,768 to -1 and from 256 to 32,767 don't "fit" into a Byte variable, an implicit conversion could result in loss of data. Therefore, Option Strict On forbids the conversion; if you want to assign x to a Byte variable, you have to make the conversion explicit, as in the following code:

 Dim x As Short = 5 Dim b As Byte = System.Convert.ToByte(x) 

Or as in the following code, which uses an intrinsic Visual Basic function:

 Dim x As Short = 5 Dim b As Byte = CByte(x) 

You can put Option Strict On at the top of your source file to turn it on or specify /optionstrict+ from the command line when you compile. This is the recommended setting, but unfortunately , the default is Off .

Many examples in this book run in a console window. To compile them using Visual Studio .NET, change the Startup Object in the project properties dialog to "Sub Main."

only for RuBoard


Object-Oriented Programming with Visual Basic. Net
Object-Oriented Programming with Visual Basic .NET
ISBN: 0596001460
EAN: 2147483647
Year: 2001
Pages: 112
Authors: J.P. Hamilton

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