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:
1 |
System.Threading.Thread.CurrentThread.CurrentCulture |
VB.NET 2005 Code:
1 |
My.Application.CurrentCulture |
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:
1 |
My.Computer.Network.IsAvailable |
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:
1 |
Partial Public Class PersonPrivate pName As StringPrivate pAge As IntegerEnd Class |
The above definition defines a class with the keyword “Partial” indicating that the definition of the class is split into multiple source files.
1 |
Partial Public Class PersonPublic Property Name() As StringGetReturn Me.pNameEnd GetSet(ByVal value As String)Me.pName = valueEnd SetEnd PropertyPublic Property Age() As IntegerGetReturn Me.pAgeEnd GetSet(ByVal value As Integer)Me.pAge = valueEnd SetEnd PropertyEnd Class |
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
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:
1 |
Public Class CustomerPrivate name As StringSub New(ByVal value As String)Me.name = valueEnd SubEnd Class |
If we want to manipulate a collection of customer objects, a typical implementation in VB.NET 2003 would be:
1 |
Dim col As New Collections.ArrayListcol.Add(New Customer("John"))col.Add(New Customer("Smith"))col.Add("A String")'We want our collection to have only Customers'but still cannot restrict the above line |
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:
1 |
Dim col As New Collections.Generic.List(Of Customer)col.Add(New Customer("John"))col.Add(New Customer("Smith")) |
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.
1 |
Public Class GenericClass(Of T)Public Sub Accept(ByVal val As T) |
1 |
End SubEnd Class |
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:
1 |
Dim strGeneric As GenericClass(Of Customer)strGeneric.Accept(New Customer("John")) |
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 :
1 |
Public Class BoxPublic Class BoxPrivate mArea As IntegerPrivate mWeight As IntegerPublic Property Area() As IntegerGetReturn mAreaEnd GetSet(ByVal value As Integer)mArea = valueEnd SetEnd PropertyPublic Property Weight() As IntegerGetReturn mWeightEnd GetSet(ByVal value As Integer)mWeight = valueEnd SetEnd PropertySub New(ByVal area As Integer, ByVal weight As Integer)mArea = areamWeight = weightEnd SubPublic Shar ed Operator +(ByVal value1 As Box, ByVal value2 As Box) As BoxDim val As New Boxval.Area = value1.Area + value2.Areaval.Weight = value1.Weight + value2.WeightReturn valEnd OperatorEnd Class |
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:
1 |
Dim b As Box = New Box(12, 32) + New Box(15, 19) |
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:
1 |
Using con As New System.Data.SqlClient.SqlConnection'Other statements using conEnd Using |
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.
Load comments