Section A.2. Changes to Programming Elements

   

A.2 Changes to Programming Elements

VB.NET has removed support for several programming elements because the underlying .NET Framework Class Library and the Common Language Runtime (CLR) contain equivalent functionality. Here are the victims and their replacements . (We discuss the class library and CLR in Chapter 4 and Chapter 5.)

A.2.1 Constants

The Microsoft.VisualBasic.Constants class in the Base Class Library defines a number of constants, such as the familiar vbCrLf constant, so these can be used as always. However, some constants, such as the color constants vbRed and vbBlue , are no longer directly supported. Indeed, the color constants are part of the System.Drawing namespace's Color structure, so they are accessed as follows :

 Me.BackColor = System.Drawing.Color.BlanchedAlmond 

In most cases, to access a particular constant that is not a field in the Microsoft. VisualBasic.Constants class, you must designate the enumeration (or structure) to which it belongs, along with the constant name . For example, the vbYes constant in VB 6 continues to exist as an intrinsic constant in VB.NET. However, it has a counterpart in the MsgBoxResult enumeration, which can be accessed as follows:

 If MsgBoxResult.Yes = MsgBox("OK to proceed?", ... 

For a list of all built-in constants and enums, see Appendix D.

A.2.2 String Functions

The String function has been removed from the language. In its place, we simply declare a string and initialize it, using syntax such as:

 Dim str As New String("A"c, 5) 

which will define a string containing five A s. Note the use of the modifier c in "A"c to define a character (data type Char), as opposed to a String of length 1. This is discussed in more detail in Chapter 2.

A.2.3 Emptiness

In VB 6, the Empty keyword indicates an uninitialized variable, and the Null keyword is used to indicate that a variable contains no valid data. VB.NET does not support either keyword, but uses the Nothing keyword in both of these cases.

According to the documentation: " Null is still a reserved word in Visual Basic .NET 7.0, even though it has no syntactical use. This helps avoid confusion with its former meanings." Whatever.

In addition, the IsEmpty function is not supported in VB.NET.

A.2.4 Graphical Functionality

The System.Drawing namespace contains classes that implement graphical methods. For instance, the Graphics class contains methods such as DrawEllipse and DrawLine. As a result, the VB 6 Circle and Line methods have been dropped.

Note that the VB 6 PSet and Scale methods are no longer supported and that there are no direct equivalents in the System.Drawing namespace.

A.2.5 Mathematical Functionality

Mathematical functions are implemented as members of the Math class of the System namespace. Thus, the VB 6 math functions, such as the trigonometric functions, have been dropped. Instead, we can use statements such as:

 Math.Cos(1) 

Note also that the Round function has been replaced by the Round method of the System.Math class.

A.2.6 Diagnostics

The System.Diagonstics namespace provides classes related to programming diagnostics. Most notably, the VB 6 Debug object is gone, but its functionality is implemented in the System.Diagnostics.Debug class, which has methods such as Write, WriteLine (replacing Print), WriteIf, and WriteLineIf.

A.2.7 Miscellaneous

Here are a few additional changes to consider:

  • The VB 6 DoEvents function has been replaced by the DoEvents method of the Application class of the System.Windows.Forms namespace.

  • The VB 6 IsNull and IsObject functions have been replaced by the IsDBNull and IsReference methods of the Information class of the Microsoft.VisualBasic namespace. Since this namespace is implicitly loaded by VB as part of the project template when a project is created in Visual Studio, no Imports statement is required, and the members of its classes can be accessed without qualification.

  • Several VB 6 functions have two versions: a String version and a Variant version. An example is provided by the Trim$ and Trim functions. In VB.NET, these functions are replaced by a single overloaded function. Thus, for instance, we can call Trim using either a String or Object argument.

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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