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.
Let’s first talk about some language innovations and in particular, some new keywords in Visual Basic 2005.
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.
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
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.
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.
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 types?Unsigned 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.
The “My” Namespace
One of the shocks that VB6 programmers encountered when they migrate to Visual Basic .NET is the sheer number of class libraries available. VB6 programmers often struggle with finding the right class for the right task, and to them this task can be like finding a needle in a haystack.
To ease the pain of VB6 programmers, Microsoft has added the new My namespace in Visual Basic 2005.
The My namespace:
- Provides shortcuts to commonly used functionality within the .NET Framework
- Adds functionality that was previously difficult to find, difficult to use, or was simply not possible without unmanaged API calls
|Figure 1. My Namespace: The figure shows the objects in the My namespace.
My includes the following objects (see Figure 1).
For example, to get the computer to play a system sound, you can directly use the My.Computer object.
My.Computer.Audio.PlaySystemSound( _ SystemSounds.Exclamation)
Contrast this to using the Win32 API to play audio, or using the Audio class within the Microsoft.VisualBasic.Devices namespace. (Who would have thought that this library was located in this namespace?)
Dim audio As New _ Microsoft.VisualBasic.Devices.Audio() audio.PlaySystemSound( _ SystemSounds.Exclamation)
Another example of using the My namespace is downloading files from the Internet. The following statement downloads an image from the Web and then saves it onto your local drive:
My.Computer.Network.DownloadFile( _ "http://www.code-magazine.com/ images/code_logo_white.gif", _ "C:Imageslog.gif")
In Visual Basic 2005, you can now split the definition of a class into multiple physical files using the new Partial keyword.
For example, you could rewrite the MyString class illustrated earlier as two separate partial classes (see Listing 2).
|Figure 2. Designer-generated Code: The designer-generated code for a Windows Form saved in a separate file.
Using partial classes you can now try to separate a class definition into multiple files, perhaps one class for containing all the properties definitions and another class for all the methods. To the compiler, the partial classes are treated as one single class.
Partial classes are useful for separating designer-generated code and business logic. For example, in Visual Studio .NET 2003, the designer-generated code for a Windows Form is embedded within the code-behind for the form. This often creates a mess when developers unknowingly modify the generated code. In Visual Studio 2005, the designer-generated code for WinForms are all saved in a separate partial class (see Figure 2), so your code behind for your Windows form is now clear of all the clutter (see Figure 3).
|Figure 3. Code Behind: The figure shows the code behind of a Windows Form.
Apart from using partial classes to separate business logic from UI, you can also use partial classes to add extra methods to classes that are auto generated, e.g., a typed dataset. You can then regenerate the auto generated part of the file without breaking the extra methods.
Generics is a new feature in the .NET 2.0 Framework. As such, languages such as C# and Visual Basic 2005 now support this new feature.
Using Generics, you can define a class definition without fixing the data type that it is using at design time. To better understand how Generics work, consider the case of a Queue class. A queue is a first-in-first-out data structure and is useful for storing messages in the order they were received for sequential processing.
Listing 3 shows that the MyQueue class defines a circular queue data structure that allows items to be enqueued (inserted) and dequeued (removed).
What is notable in this class is that I have not fixed the specific data type of the items that can be inserted into the queue. Rather, I have used the Of keyword as a placeholder for the actual data type to be used.
Public Class MyQueue(Of itemType)
To use the MyQueue class with Integer values, simply instantiate the MyQueue class with Of Integer (in this case, q1 is known as a constructed type).
Dim q1 As New MyQueue(Of Integer)(3) q1.Enqueue(5) q1.Enqueue(6) q1.Enqueue(7) MsgBox(q1.Dequeue) ' prints 5 MsgBox(q1.Dequeue) ' prints 6 MsgBox(q1.Dequeue) ' prints 7 q1.Enqueue("A string") ' error
If you want to use the MyQueue class with string values, then use Of String as the parameter.
Dim q2 As New MyQueue(Of String)(3) q2.Enqueue("Microsoft ") q2.Enqueue("VB 2005 ") q2.Enqueue("rocks!") MsgBox(q2.Dequeue) 'prints "Microsoft" MsgBox(q2.Dequeue) 'prints "VB 2005 " MsgBox(q2.Dequeue) 'prints "rocks!" q2.Enqueue(5) ' error
The advantage of using generics for this example is that I do not need separate classes for different types of data types. Of course, one way would be to use an Object data type for the items inserted into the queue and during runtime perform type casting to get the required data type. However, doing so results in late binding and affects performance and type-safety.
|Figure 4. Code Snippets: Inserting code snippets is easy.
To summarize, generics affords the following:
- Type Safety. Generic types enforce type compliance at compile-time, and not run-time (as in the case of using Object). This reduces the chances of data-type conflict during run time.
- Performance. The data types to be used in a Generic class are determined at compile time, hence there is no need to perform type casting during run time, which is a computationally costly process.
- Code Reuse. Since you only need to write the class once and customize it to use with the various data types, there is a substantial amount of code-reuse.
In an attempt to make Visual Basic more accessible to a wider developer base, Microsoft has made a very innovative addition to Visual Studio 2005?code snippets.
Code snippets are small, commonly-used blocks of code. Code snippets allow you to insert commonly-used code blocks into your project, thereby improving the efficiency of your development process. This is very useful for developers who are new to the language and need a way to jumpstart their development.
|Figure 5. Inserting Code Snippets: The figure shows the process of Inserting a code snippet to encrypt a string.
In the final release of Visual Studio 2005, it is expected that Microsoft will ship more than 500 code snippets, but in the current release (Beta 1 at the time of writing) there are only about 30 or so code snippets.
To insert a code snippet into your code, simply right-click in Code View and select “Insert Snippet…” (see Figure 4).
You can then choose the code category that suits your purpose. For example, if you choose Security, you will see a listing like that shown in Figure 5.
|Figure 6. Inserted Code Snippet: The inserted code snippet with the placeholder.
If you choose “Decrypt a String,” a block of code would then be inserted into your application. To customize the code, simply replace the placeholder highlighted in yellow (see Figure 6).
Code snippets are stored in the C:Program FilesMicrosoft Visual Studio 8VbSnippets1033 folder. They are stored as XML files so you can write your own code snippets to be shared among your colleagues.
XML Code Commenting
XML code commenting was long supported in languages like C# and Java, and it is finally supported in this new release of Visual Basic.
|Figure 7. XML Comments: The figure shows inserted XML comments.
XML code commenting allows you to use XML elements to document your code. To insert an XML comment, simply type three apostrophes before a class or method definition and the XML code templates will automatically be inserted for you (see Figure 7).
Once you have documented your code with XML elements, an XML document containing all the comments in your code would be generated when you build your application. This XML document could then be processed by a third-party tool so that a help file may be generated. NDoc is one such tool that helps to generate documentations for .NET assemblies. For a detailed look at how to use NDoc to generate professional-looking documentation, checkout my MSDN article, “Generating Documentation for Your Visual Basic .NET Applications“.
Besides the language innovations, the code editor in Visual Studio 2005 is also significantly improved to simplify Visual Basic development.
|Figure 8. Autocorrect: The figure shows how Autocorrect suggests remedies for an error.
In Visual Studio .NET 2003, if a statement contains an error, the offending keyword within that statement would be highlighted, together with the reason for the error. In Visual Studio 2005, Microsoft has gone one step further with the Autocorrect feature.
The Autocorrect feature tries to analyze the cause of the error and make suggestions. For example, you may have remembered to add a reference to the System.Data.dll for ADO.NET data access in your project, but have forgotten to import the System.Data namespace. In this case, if you try to declare a SqlDataReader variable, Visual Basic will flag the SqlDataReader class as not defined. Using Autocorrect, you can place your cursor over the underline and click the exclamation icon in red to view a list of suggested remedies (see Figure 8).
|Figure 9. Exception Assistant: The figure shows the Exception Assistant in action.
In this case, you can either use a fully qualified name for the SqlDataReader class, or simply add an imports statement at the top of the code.
Another improvement in Visual Studio is the new Exception Assistant. The Exception Assistant will kick into action when a runtime error occurs. For example, in Figure 9 I have forgotten to add the New keyword to the instantiation statement, and using the Button object without instantiation will cause a runtime error. While this is a common error, it may take a Visual Studio .NET beginner a while to figure it out. The Exception Assistant tries to suggest ways to remedy the runtime error.