New Features in VB.NET Whidbey, Part 1by Wei-Meng Lee, author of Windows XP Unwired
Microsoft recently released the Community Preview of the next Visual Studio .NET (codenamed Whidbey) With so many changes in the pipeline (many of which are improvements to the current .NET 1.1 way of doing things), developers would do well to get their learning engines up and running early so that they can be well-oiled when Microsoft finally launches Visual Studio .NET 2005. One of the enhancements in Whidbey is the new improved VB.NET language. Based on feedback on VB.NET, Microsoft has added some new features to the language. In this article, I will walk you through some of the enhancements in VB.NET. In the next article, I will focus on one of the new enhancements to .NET languages: generics.
VB.NET Whidbey introduces three new unsigned data types,
UShort, which take up four, eight, and two bytes, respectively. They are useful in situations where you do not have the need to store negative numbers, and hence can use unsigned data types to save on storage. For example,
UInteger is four bytes, so instead of using
Long, which is eight bytes, you can store positive numbers (up to a maximum of 4,294,967,295).
The following shows the comparison between an unsigned integer versus an
Integer data type:
Dim UInt As UInteger Dim int As Integer int = -2147483648 ' minimum value int = 2147483647 ' maximum value UInt = -1 ' not allowed UInt = 0 ' minimum value UInt = 4294967295 ' maximum value
Instead of defining a class in a single file, you now have the option to split the class definition into multiple files. This is useful if you need to hide some sensitive information within a class definition. For example, the Windows Form Designer uses partial classes for controls such as Form, so that it can hide the
InitializeComponent procedure. Since the user should never modify the sensitive code in a procedure, it is much safer for the
InitializeComponent procedure to be defined in another location.
The following example illustrates the use of the
' defined in file1 Public Class Point Dim pX, pY As Single Property x() Get Return pX End Get Set(ByVal value) pX = value End Set End Property Property y() Get Return pY End Get Set(ByVal value) pY = value End Set End Property End Class ' defined in file2 Partial Public Class Point Public Function getDistanceFromO() As Single Return Math.Sqrt(pX ^ 2 + pY ^ 2) End Function End Class
VB.NET simply combines the two definitions into one, and it is transparent to the user:
Dim ptA As New Point ptA.x = 3 ptA.y = 4 MsgBox(ptA.getDistanceFromO())
Note that the
Partial keyword applies also to structures.
Another new feature in VB.NET is operator overloading. For example, the
+ operator can be overloaded to add other data types, besides adding numbers and concatenating strings. In the following example, I have overloaded the
+ operator to add the lengths of two
Public Class Point Public Shared Operator +(ByVal pointA As Point, _ ByVal pointB As Point) _ As Single Return (pointA.getDistanceFromO + _ pointB.getDistanceFromO) End Operator End Class
So, I could essentially do this:
Dim ptA As New Point Dim ptB As New Point ptA.x = 3 ptA.y = 4 ptB.x = 4 ptB.y = 5 MsgBox(ptA + ptB) ' returns 11.40312
IsNot operator is a new keyword that has been added to VB.NET. Instead of using the sometimes awkward combination of the
Not operators to compare the equality (or inequality) of two objects, it is much more natural to combine the two into one, as the following example illustrates:
Dim ds As New DataSet ' sounds awkward If Not ds Is Nothing Then MsgBox("ds contains a reference") End If ' sounds better! If ds IsNot Nothing Then MsgBox("ds contains a reference") End If
VB6 programmers who migrated to VB.NET often lament at the need to create an instance of a form before they can display the form. In VB.NET, you need to do the following when opening another form:
'---uses another instance of it Dim frm2 As New Form2 frm2.Show()
In VB.NET Whidbey, there is no need to create an instance of another form before you can display it. You can open it directly (just as you can in VB6) using its default instance:
'---default instance Form2.Show() ' uses the default instance
Of course, if you wish to create additional instances of Form2, you can use the former method.
Continue statement allows you to skip to the next iteration in a loop. For example, the following will sum up all of the odd numbers from 0 to 10:
'---continue statement Dim counter, sum As Short sum = 0 For counter = 0 To 10 If counter Mod 2 = 0 Then Continue For sum += counter Next
Continue keyword works for the
While looping constructs.
Explicit Zero Lower Bound on an Array
Another new keyword in VB.NET is
To. For example:
'---new "To" keyword Dim num(5) As Integer Dim num(0 To 5) As Integer ' same as above
To keyword serves cosmetic purposes only; it does not allow you to change the lower bound of an array:
Dim num1(1 to 5) as Integer ' not allowed
To keyword helps to make your code more readable; beginning VB.NET programmers (especially C++ and Java programmers) often did not realize that the number of array members is always one more than the index specified in the array declaration.
Often you need to create and use some resources and then immediately release the resources so that memory can be conserved. VB.NET Whidbey comes with a new construct known as
End Using. The
Using construct guarantees that the resources defined within the
Using block will be disposed of after the block:
Public Sub DataAccess(ByVal str As String) Using conn As New SqlConnection(str) Dim ds As DataSet '---some code to perform data access End Using ds = Nothing '---not allowed as ds is ' defined within the using block End Sub
VB.NET Whidbey provides the new
My object that encapsulates some of the most common functionality that developers need in their daily work. The
My object exposes several different objects, as shown in Figure 1. Note that at this moment, some of the objects exposed by the
My object are not yet finalized, and are subject to changes.
Figure 1. Using the
The aim of the
My object is to provide a quick way for developers to access information they need about their computer/application without needing to create an instance of an class. (It also serves as a one-stop shop for locating all of the relevant classes.) For example, the following example shows how I can check to see if my computer is connected to a network, and if it is, print out the IP address straightaway:
If My.Computer.Network.IsConnected Then MsgBox(My.Computer.Network.IPAddress) End If
I can also check the current UI culture of my application, which is needed for localization:
I hope that you now have a good idea of some of the language enhancements that will ship with Visual Basic .NET Whidbey. In the next article, I will discuss the next big enhancements to VB.NET: generics.
Wei-Meng Lee (Microsoft MVP) http://weimenglee.blogspot.com is a technologist and founder of Developer Learning Solutions http://www.developerlearningsolutions.com, a technology company specializing in hands-on training on the latest Microsoft technologies.
Return to ONDotnet.com