
f you have been a hardcore VB6 programmer and you've hesitated about switching to Visual Basic .NET, Visual Basic 2005 will change your mind and you'll want to take the leap forward to move to .NET.
The common complaint that people who have made this leap already often hear from programmers who are reluctant to move to VB .NET is that it is not VB-like, and moving to .NET means you have to unlearn many of the things you have painstakingly mastered in VB6.
While the next release of Visual Basic will not allow you to bring back your VB6 days of glory, Microsoft knows the concerns VB6 programmers have and they've made a serious attempt in this new release to ease the learning curve. In this article, I will walk you through some of the new features in Visual Basic 2005 and I'll tell you how it will simplify your life as a developer.
New Keywords
Let's first talk about some language innovations and in particular, some new keywords in Visual Basic 2005.
Continue
The new
Continue keyword allows you to skip an iteration in a loop. Consider the following snippet.
For i As Integer = 0 To 10
' prints out all odd
' numbers from 0 to 10
If i Mod 2 = 1 Then
MsgBox(i)
Else
Continue For
End If
Next
This code segment prints out all the odd numbers from
0 to
10. You can also use the
Continue keyword in both a
While loop and
Do-While loop.
IsNot
Visual Basic includes a new comparison operator known as
IsNot. Often you need to negate the comparison of an object, such as in this snippet.
Dim obj As Button
If Not obj Is Nothing Then
' obj contains an object reference
....
End If
In this case, your code would be more readable if you use the
IsNot operator.
If obj IsNot Nothing Then
' obj contains an object reference
....
End If
To
Note that you cannot change the base index (of an array) of 0 to something else; the use of the To keyword is purely to enhance readability.
|
|
Another new keyword in Visual Basic 2005 is the
To keyword. The
To keyword is used in an array declaration. Consider the following.
Dim numArray(50) As Integer
The above declaration may be mistaken by C, C#, C++, and Java programmers as declaring an integer array of 50 elements. In fact, in Visual Basic, it is actually 51 elements. The new
To keyword will clarify this.
Dim numArray(0 to 50) As Integer
In this case, your code explicitly declares the array indices to be from 0 to 50 (51 elements in total). This will hopefully prevent confusion and aid readability.
Using
Often you need to create and use some resources and then immediately release the resources in order to conserve memory. Visual Basic 2005 comes with a new construct known as
Using. The
Using construct guarantees that the resources defined within the
Using block will be disposed of after the execution of the block. Consider the following snippet.
Public Sub Data_Access( _
ByVal str As String)
Using conn As New SqlConnection(str)
Dim ds As DataSet
'---some code to perform data access
End Using
...
End Sub
The
conn and
ds variables are valid only within the
Using block. In fact, all variables declared within the block (
ds, in this case) will be disposed after the
Using block executes. The
Using block is a good way for you to ensure that resources (especially COM objects and unmanaged code, which would not be unloaded automatically by the garbage collector in the CLR) are properly disposed of after they are no longer needed.
Operator Overloading
In Visual Basic 2005, you now not only can overload methods, you can also overload operators. Consider the MyString class (see
Listing 1).
Within this class you overload the "-" operator so that you can perform a minus operation on two MyString objects.
Dim s1 As New MyString("Visual Basic .NET")
Dim s2 As New MyString("2005")
MsgBox(s1 - s2)
' prints Visual Basic 2005
New Data Types
In Visual Basic 2005, you can now use the three new unsigned data typesUnsigned Integer (
UInteger), Unsigned Short (
UShort), and Unsigned Long (
ULong). The following shows some examples of the new unsigned data types.
Dim num1 As UInteger
' range: 0 to 4294967295
num1 = 4294967295
Dim num2 As UShort
' range: 0 to 65535
num2 = 65535
Here's a list of data types in Visual Basic 2005 and their storage allocation:
- Boolean (Depends on implementing platform)
- Byte (1 byte)
- Char (single character) (2 bytes)
- Date (8 bytes)
- Decimal (16 bytes)
- Double (8 bytes)
- Integer (4 bytes)
- Long (8 bytes)
- Object (4 bytes)
- SByte (1 byte)
- Short (2 bytes)
- Single (4 bytes)
- String (Depends on implementing platform)
- UInteger (4 bytes)
- ULong (8 bytes)
- UShort (2 bytes)
- User-Defined Structure (Depends on implementing platform)
Resurrection of VB6 Features
In VB6, you can open a new Windows form by calling its
Show() method directly:
Form2.Show(); however, in Visual Basic .NET, VB6 programmers had a rude awakening when they discovered that they need to create an instance of a form before they are able to open it:
Dim frm2 As New Form2
frm2.Show()
This is because Visual Basic .NET is an object-oriented language and hence a form must be instantiated before it can be loaded. Microsoft has heard the feedback of VB6 programmers and in Visual Basic 2005, you can now load a form (using its default instance) without creating an instance of it:
Another feature resurrected from VB6 was the much sought-after edit-and-continue. In earlier releases of Visual Basic .NET, edit-and-continue was not supported, but it is in Visual Basic 2005.