Visual Basic.NET 2005

Discover some of the new features in Visual Basic.NET 2005 such as partial classes, generics, operator overloading and using block

New features in Visual Basic.NET 2005
build on platform’s power and efficiency

Although Microsoft.NET languages have more or less the same facilities and power, one must be aware of the syntax and capabilities of a language to take full advantage of it. This article introduces new features of the upcoming Visual Basic.NET language that is part of Visual Studio.NET 2005.

Visual Basic is one of the most popular languages used in the software development industry. Its popularity comes from simplicity and efficiency. The new version of Visual Basic.NET builds upon these attributes and introduces the following useful new features:

  • my namespace
  • partial classes
  • generics
  • operator overloading
  • using block

My namespace

The My namespace feature includes functions and properties to do a lot of complex things in a rapid manner. Think of this as shortcut syntax to get things done with less code. It groups commonly required information and functionalities for easier access. The examples below show how My namespace retrieves the current culture of the application compared to VB.NET 2003:

VB.NET 2003 Code:

VB.NET 2005 Code:

Even actions such as accessing various properties of the computer can be done easily and quickly with My namespace. The following code, for example, will return a boolean indicating the availability of the network:

Partial classes

Partial classes define a class in multiple files. This separation is useful when you want to have functionality of a class coded with different concerns and using inheritance might not be appropriate.

Partial classes are used in Visual Studio.NET 2005 for auto-generated code. When you create a windows form, for example, the Visual Studio.NET 2005 IDE creates a lot of auto-generated code. In previous versions, this code is wrapped in regions in the same source file as event-handling code. In Visual Studio.NET 2005, auto-generated code is separated from event-handling code by defining it in a separate source file as a partial class. Here’s an example of how a partial class can be defined:

The above definition defines a class with the keyword “Partial” indicating that the definition of the class is split into multiple source files.

Now we are providing more definition of our Person class in a separate source file, providing properties to expose our private fields.

Partial class can also be used to make your datasets more intelligent. The typed datasets you create in Visual Studio.NET have the partial class definition for the generated source. This enables you to add value to your typed dataset by providing functionality such as business logic by defining a partial class definition for the generated class.


Generics enable your program to adapt itself to different types. The concept of generics is used in the implementation of collections in the new framework. Let’s define a simple customer class as follows:

If we want to manipulate a collection of customer objects, a typical implementation in VB.NET 2003 would be:

The problem with this implementation is even though you wanted your collection to only handle customers, there is no way of enforcing this. To restrict your collection to handle only customers, you will need to implement a custom collection class by extending the facilities provided by the collection framework. But what if you want to implement different collections to handle different types of objects? You might end up with a lot of custom collections to handle different types of objects even though the functionality you require from these collections is the same.

This is where generics come in. They give you a way to implement a placeholder for the type and then specify what the type is. An ideal solution to the previous problem would be to provide the implementation of the collection with the type of the collection as a variable; when creating a collection we would specify what the collection should manipulate. That’s exactly the functionality of the collections provided by the System.Collections.Generic namespace in Visual Basic.NET 2005:

If you try to add a string or anything other than customer objects, the compiler will detect it as a syntax error. You can also define your own generic functionality to program to types that are not known in advance or to enable your program to work with multiple types.

The above class is defined by using the variable “T” as a placeholder. When you create an instance of GenericClass you replace the placeholder type with a real type. Once we do this, our Accept method will accept only arguments of the real type defined while instantiating GenericClass. For example:

Operator overloading

Operator overloading enables you to specify what operators such as + or – should do when invoked with objects of classes you define. For example :

The above class definition is a simple box with an area and weight as properties. We wanted to provide functionality to add two box objects and return a box with the area and weight as the total of added boxes. We have overloaded the meaning of the + operator to work with Box objects by defining how it should work. We could have defined a new method with a name to achieve this, but using operators for similar operations makes the code more readable and easy to use. To add two box objects, we simply use the + operator in a way similar to adding two numbers:

Using block

Some objects you create are very resource intensive. So a good practice is to release the resource as soon as you are finished with it. Generally when you create an object inside a method, the object will not be released until the method terminates. But what if you want an easy way to define an object as well as specify the scope of it inside a method? The using block helps you to achieve this:

In the above code, the scope of the defined con object will be limited within the using block and will be released as soon as it exits that block.

Rich and rapid

Visual Basic.NET is one of the popular languages in the .NET platform and future versions are making it richer without losing the rapid development approach for which it’s renowned. This article should help users take immediate advantage of new features that will be an integral part of Visual Studio.NET 2005 and the Microsoft.NET 2.0 platform.