VB .NET Language in a Nutshell: What's New and Different in VB .NET
Pages: 1, 2, 3, 4, 5
Miscellaneous Language Changes
VB .NET includes several miscellaneous changes that include the
format of line numbers, the lack of support for the GoTo and GoSub statements, and
the replacement of the Wend keyword by End While.
Line numbers
Visual Basic .NET requires that every line number be followed
immediately by a colon (:). A statement can optionally follow the colon. In VB
6, line labels, which were used in particular for error handling by the On ErrorGoTo statement, had to be followed immediately by a
colon, but line numbers did not.
On GoTo
The On...GoSub and On...GoTo constructions are not supported. However, VB .NET
still supports the OnError GoTo statement.
While
The While...Wend construction loops through code while a specified
condition is True. VB .NET retains that
construction, but replaces the Wend keyword with
the End While statement.
The Wend keyword is not supported.
|
Related Reading
|
GoSub and Return statements
In VB .NET, the GoSub statement is not supported.
As remarked earlier, in VB .NET, the Return statement is used to return control to the calling program from a function or subroutine. The VB 6
Exit Sub and Exit Function statements
continue to be supported in VB .NET; however, the advantage of the Return statement is that it allows you to specify the
function's return value as an argument to the Return statement.
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 Chapters and .)
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.
String Functions
The LSet and RSet functions have been replaced by the PadLeft and PadRight methods of the System.String class. For instance, the following code pads the name "Donna" with spaces on the left to make the total string length equal to 15:
Dim sName As String = "Donna"
Msgbox(sName.PadLeft(15))
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 As. 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.
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.
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.
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 Round method of the System.Math class.
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. (You won't believe it, but there is still no method to clear the Output window!)
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
Importsstatement 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.
Obsolete Programming Elements
The following list shows some of the programming elements that have been removed from Visual Basic .NET:
As Any- Required all parameters to have a declared data type.
- Atn function
- Replaced by System.Math.Atan.
- Calendar property
- Handled by classes in the System.Globalization namespace.
Circlestatement- Use methods in the System.Drawing namespace.
- Currency data type
- Replaced by the Decimal data type.
- Date function
- Replaced by the Today property of the
DateTimestructure in the System namespace. Datestatement- Replaced by the
Todaystatement. - Debug.Assert method
- Replaced by the Assert method of the Debug class of the System.Diagonistics namespace.
- Debug.Print method
- Replaced by the Write and WriteLine methods of the Debug class of the System.Diagonistics namespace.
Deftypestatements- Not supported.
- DoEvents function
- Replaced by the DoEvents method of the Application class in System.Windows.Forms namespace.
Emptykeyword- Replaced by the
Nothingkeyword. Eqvoperator- Use the equal sign.
GoSubstatement- Not supported.
ImpoperatorAImp Bis logically equivalent to(Not A)Or B.- Initialize event
- Replaced by the constructor method.
- Instancing property
- Use the constructor to specify instancing.
- IsEmpty function
- Not supported because the
Emptykeyword is not supported. - IsMissing function
- Not supported because every optional parameter must declare a default value.
- IsNull function
- Not supported. The
Nullkeyword is replaced byNothing. - IsObject function
- Replaced by the IsReference function.
Letstatement- Not supported.
Linestatement- Use the DrawLine method of the Graphics class in the System.Drawing namespace.
LSetstatement- Use the PadLeft method of the String class in the System namespace.
Nullkeyword- Use
Nothing. On...GoSubconstruction- Not supported. No direct replacement.
On...GoToconstruction- Not supported. No direct replacement.
On Error...is still supported, however. Option Basestatement- Not supported. All arrays have lower bound equal to 0.
Option Private Modulestatement- Use access modifiers in each individual
Modulestatement. Property Get,Property Let, andProperty Setstatements- Replaced by a new unified syntax for defining properties.
- PSet method
- Not supported. No direct replacement. See the System.Drawing namespace.
- Round function
- Use the Round method of the Math class of the System namespace.
RSetstatement- Use the PadRight method of the String class in the System namespace.
- Scale method
- Not supported. No direct replacement. See the System.Drawing namespace.
Setstatement- Not supported.
- Sgn function
- Use Math.Sign.
- Sqr function
- Use Math.Sqrt.
- String function
- Use the String class constructor with parameters.
- Terminate event
- Use the Destroy method.
- Time function and statement
- Instead of the Time function,
use the TimeOfDay method of the
DateTimestructure of the System namespace. Instead of theTimestatement, use theTimeOfDaystatement. Typestatement- Use the
Structurestatement. - Variant data type
- Use the Object data type.
- VarType function
- Use the TypeName function or the GetType method of the Object class.
Wendkeyword- Replaced by
EndWhile.
Structured Exception Handling
VB .NET has added a significant new technique for error
handling. Along with the traditional unstructured error handling through On ErrorGoto statements, VB .NET adds structured exception handling, using the Try...Catch...Finally syntax supported by
other languages, such as C++. We discuss this in detail in Chapter 7.


