Overview of Changes from VB6 to VB.NET, Part 2
By Bipin Joshi
In Part 1 we examined VB.NET's changes to data types, variable declaration, and arrays. In this part we'll continue our examination of VB.NET's syntax changes.
Variable Scoping
Consider following VB6 code:
|
The above code runs perfectly with VB6 because there is no block-level variable scoping. (Block-level scoping
occurs in other modern programming languages (such as C++). Variables defined within statement blocks, such as the
variable defined in the If ... Then block, fall out of scope when the statement block ends. Therefore,
accessing z outside of the If ... Then block where it's defined, would cause an error
in modern programming languages.) VB.NET, contrary to VB6, employs block-level variable scoping.
The Set and Let Statements
In VB6 you must use the Set statement to assign an object instance to a variable. This was necessary
in VB6 because of default properties. To tell VB that we want to assign a variable to the object itself (as opposed
to the value of the object's default property) you had to use the Set keyword. For example, in VB6:
|
In VB.NET, however, there are no default properties allowed (except for parameterized properties) and hence there
is no need for the Set keyword. (Likewise, the Let keyword has been removed from the
VB.NET syntax.)
Error Handling
Visual Basic has finally incorporated structured error handling. The keywords Try, Catch
and Finally make error handling easy and make VB.NET at par with languages like C++ or C#.
This Try ... Catch model allows developers to place code that may cause an exception within
a Try block. If that code throws an exception (synonymous to raising an error), the code in the
Catch block is executed; the code in this block should be designed to gracefully handle the
exception.
Note that VB6's older error handling techniques (On Error Resume Next and the like) are still supported for
backward compatibility, although when writing new applications with VB.NET you should valiantly strive not to use
these old techniques. The following code snippet illustrates various error handling techniques with VB.NET:
|
The above code simply "catches" any exceptions caused by offending code inside the associated Try
block. VB.NET allows you to handle specific exceptions by using multiple Catch blocks:
|
In addition to catching predefined exceptions you can also create your own custom exception classes that
inherit from the System.Exception base class. You can also 'Throw' your own exceptions (similar to
using the Raise method of the Err object in VB6:
|
In Part 3 we'll conclude our examination of VB.NET's new features.




