What Is New in Visual Basic 2005
I recently got my hands on the Visual Studio trial CD that I received at one of the Ready Launch Tours. I quickly installed Visual Basic 2005 and started looking around for new features. I was quite surprised to see that Microsoft has added so many new (cool) features to Visual Basic that all other versions of Visual Basic (right from Classic to Visual Basic .NET 2003) lacked. Although there are numerous articles on the Internet describing just what is new in Visual Basic 2005, in this article I will try to concentrate more on the features that earlier required lot of coding: calling APIs, creating hooks, and so forth. Keep in mind that this article just contains a partial list of changes that have been made in Visual Basic 2005. There have been numerous changes made to Visual Basic 2005; those changes can be found on MSDN.
One of the biggest changes that Microsoft has done to Visual Basic 2005 is that it now fully conforms to Microsoft's new Common Language Specification (CLS). The new compiler generates warnings if your code is not CLS compliant. Visual Basic 2005 now supports eleven different primitive CLS-compliant Data types; these are Byte, Int16, Int32, Int64, Single, Double, Boolean, Char, Decimal, IntPtr, and String. Visual Basic 2005 also supports signed as well as unsigned integers.
I will start with simple changes and move on to some of the advanced features.
Visual Basic 2005 comes with the good old Temporary projects. In Visual Basic .NET 2003/2002, the moment a developer wanted to create a new project, he/she had to give the disk location where the project will be saved. However, in earlier versions of Visual Basic there was no need to give the Disk Location prior to creating a project; Visual Basic used to create a temporary project that the developer could save or discard depending on his/her choice. Now, Visual Basic 2005 comes with temporary project; this means you can create a new project without specifying the disk location for the project and you can experiment with it. When creating a new project, you will simply select the project type and template and specify the name of the project in the New Project Dialog box. Later, you can choose to save or discard the project. You can enable or disable this setting by selecting Options from the Tools menu, expand the Project/Solutions node, and check or uncheck "Save new projects when created", depending on your choice. A solution can contain only one Temporary project at a time.
Edit & Continue While Debugging
The feature that every programmer programming in Visual Basic liked was the beauty of Visual Basic to edit during debugging and continuing with the changes without restarting or rebuilding the solution. This feature was taken away in Visual Basic .NET 2002 & 2003. In Visual Basic 2005, Microsoft has re-introduced this feature. Now, you can edit the code during run-time in the break mode, make some changes in the code, and run it again. This feature is very time saving and enables us to make changes to the source code while the application is in break mode. And, when you resume the execution of the program, Edit and Continue will apply the code changes that were done automatically, with some limitations.
Microsoft has also re-introduced the immediate window; even this feature was not available in earlier .NET versions. You can use Immediate window to debug and evaluate expressions, execute statements, display variable values, and so forth. The Immediate window also supports Intellisense. To view Immediate window, select Windows from the Debug menu and then select Immediate. The new feature that has been introduced in Visual Studio Professional/Standard/Team editions is that you can debug during design time too. This can be done by using the Immediate window. To issue a Visual Studio command in the Immediate window, you must preface the command with a greater than sign (>). To enter multiple commands, switch to the Command window.
Continue Statement for Loops
Remember the complex conditional statements that you used to use if you had to continue the loop without executing statements inside the loop when a specific condition was met? I have always wanted the Continue statement in Visual Basic; finally, Microsoft has added it to Visual Basic 2005. Microsoft has added continue statements for all types of loops. In the For Next Loop, you can use Continue For, Continue Do in the Do..Loop and Continue While in the While..Wend Loop. Here is a small illustration of a For loop:
'print all odd numbers using For Loop Dim loopCounter As Long For loopCounter = 1 To 100 If loopCounter Mod 2 = 0 Then Continue For End If Debug.Print(loopCounter.ToString) Next
Similarly, you can use Continue While in While loops and Continue Do in Do loops. In the above code, you might have observed that Debug.Print statement also has been brought back.
Sometimes, you have to compare two objects using the Is keyword. And, if you are trying to check whether this object is not the same as the other object, you use something like this:
If Not objA Is objB Then
Although there is nothing wrong with this type of code, doesn't it seem a little bit difficult to understand at the first go? To resolve this, Microsoft has added IsNot to Visual Basic 2005. Now, you can write the same code in this way to make it more readable and easier to understand.
If objA IsNot objB Then
Code snippets are the pre-authored pieces of code that can be inserted in to the application with Visual Studio 2005. This feature is available in all the languages that are supported by Visual Studio 2005. These code snippets increase productivity by reducing the amount of time spent typing the same code again and again. You can insert Code snippets by using the Code Snippet Picker or by typing the shortcut name of the snippet and pressing Tab. Another way to insert the code snippet is to right-click in the code window, select Code Snippet, and navigate to the respective Code snippet by using the Intellisense popup.
In Visual Basic projects, you can type the first few letters of the shortcut name followed by a question mark (?), and press Tab to see a list of all snippet shortcuts that begin with those letters.
Code snippets can be managed by using the Code Snippet Manager that can be opened from the Tools Menu in Visual Studio. Code Snippets are saved as XML files in the following location:
- Visual Basic 2005: Program Files\Microsoft Visual Studio 8\Visual Basic
- Visual Basic Express 2005: Program Files\Microsoft Visual Studio 8\Common7\IDE\VBExpress\Snippets\1033
You can add new code snippets by using the Code Snippet Manager or even search for code snippets online. With Code Snippets, Microsoft has added more code reusability in .NET 2.0.
Sometimes, code requires you to access an unmanaged resource such as a COM wrapper, a SQL Connection, and so on. C# developers had an advantage by using Using Block; that assured them of disposing the resources the moment the code exits the block. This now has been introduced in Visual Basic 2005. The Using block can be used to dispose of unmanaged resources while managed resources are disposed of by the .NET Frameworks Garbage Collector without any extra coding. The Using bock behaves in a similar fashion as the Try..Finally construction. The Try block uses the resources and the Finally block disposes them. Using that block guarantees the disposal of the resources no matter how you exit the block—even in the case of an unhandled exception, except for the StackOverFlowException. To know more about the syntax of Using Statement check out MSDN.
Application Level Events
Visual Basic 2005 now comes with Application-level events that are very useful. Visual Basic now provides a well-defined application model to control the behavior of the Win Forms applications. This model is known as the Visual Basic Application Model. This model includes events for handling Startup and Shutdown of the application, catching any unhandled exception, checking network availability, and creating single-instance applications. Code for these events is stored in a hidden ApplicationEvents.vb file. To access the Code Editor of the Application Events, you can select Application Page from the Project Properties and click theView Application Events button. This will open the code editor for the ApplicationEvents.vb file.
Now, take a brief look at these events:
This event is raised the moment the application starts, before loading the main/splash form. In this event, you can add code to perform any startup initialization. The Startup event also provides you with the way to cancel the application's execution. The StartupEventArgs object contains the Cancel property that can be set to true if you want to cancel the application's execution. To cancel the execution of the application, you can use e.Cancel = True. StartupEventArgs also contains the CommandLine arguments that are passed to the application from the commandline.
The Shutdown event is triggered when the application is about to terminate. The Shutdown event signals that the application is about to shut down. This event can be used to perform operations such as saving, closing connections, releasing any resources, writing a log entry, and the like.
You might have observed how annoying it is when you see a run-time error in a production environment and when you click OK, the application terminates. All that the user had entered would be gone and everyone will be shouting at the developer saying where has the data gone? Visual Basic 2005 now provides an easy and efficient way to handle all the Unhandled exceptions at the application level. To me, this is the one of best things that has happened to Visual Basic 2005. Now, if an application raises an exception and if there is no code written to handle the exception, the application will automatically raise the MyApplication_UnhandledException event. The handler that is written in the UnhandledException event can handle that exception and perform the necessary actions. You can even exit from the application or continue the execution of the application. This event contains the UnhandledExceptionEventArgs object that contains information about the unhandled exception.
This is another event that is very useful when writing Client/Server applications. The NetworkAvailabilityChanged event is raised when there is change in the network availability. You can write any code to handle the change in the Network Availability in this event. You can use the Boolean IsNetworkAvailable property of the e parameter to check the state of the network connection.
Apart from this event, if you want to check the Network Connection status in the rest of your application, you also can use the My.Computer.Network.IsAvailable property.
This event occurs when you try to execute another instance of the already active application. When a single-instance application starts for the first time, it raises the StartupEvent; however, when the same application is executed again with the already running application being active, it raises the StartUpNextInstance event and does not raise the StartUpEvent. You will take a closer look at this event in the next section.
Making Your Application Single Instance
Making an application single instance in Visual Basic .NET 2003 always required writing extra code. This has been made so easy in Visual Basic 2005; you just need to check the check box and your application will be compiled as a Single Instance application. To make your application Single Instance, select Project Properties from the Project Menu. In the resulting Page, select the Application tab and check the Make Single Instance checkbox under Windows Application Framework properties. That is all you need to do. In the application events section, I had written about the StartUpNextInstance event. This event is raised when you try to run an application that is marked as Single Instance and is already running. This event can be used to get the command line parameters that are passed to the second instance and/or can also be used to bring the already running instance to foreground. To bring the already running instance to foreground, you can use set the BringToForeGround property of e(StartupNextInstanceEventArgs parameter) to true. You can also get the command line parameters that are passed to the second instance by accessing the CommandLine Property of the same parameter.
In previous versions of Visual Basic .NET, the moment you closed the main Form, the whole application would terminate even if there were other forms open. This sometimes created problems and needed a workaround. Now, in Microsoft Visual Basic Application Model, Microsoft has added one more feature wherein you can mention whether the application needs to be terminated when the startup object/form is closed or when the last open form is closed. This can be achieved by opening Project Properties from Project menu and selecting the Application Tab. In the resulting Page, Shutdown Mode can be selected by using the combobox that contains two options, namely:
- When the startup form closes (this is default)
- When the last form closes
If you select the second option, the application ends only when the last form is closed or when the My.Application.Exit or End statement is called explicitly.
With all these new features, Visual Basic again has become a language that can quicken the speed of development and writing code. Now, developers can concentrate on writing business logic rather than spending some of their time writing workarounds for different tasks that were built into Windows. In my next article, I will take a look at the mysterious magical My object that has made life easier for Visual Basic developers.