RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


What's New in Visual Basic 9.0? Part 1 : Page 4

The release of Visual Studio 2008 updates Visual Basic to version 9.0, with several key language enhancements and LINQ support. Part one of this series will walk you through using nullable types, type inference, the If operator, and other time-saving enhancements.

Extension Methods
In the past, to add additional methods to a class, you had to subclass it and then add the methods to the subclass. In VB 9.0, you can just use the new extension methods feature to add a new method to an existing CLR type.

To see how extension methods work, consider the following example: suppose you deal very frequently with the Point class and you want to be able to quickly find out the distance between two points. In this case, you might be better served if you added a new function called DistanceFromThisPointTo() to the Point class, allowing all instances of the Point class to make use of this method. To do so, define a new module (you need to import the System.Runtime.CompilerServices namespace) and define the extension method (a function) within it, like this:

Imports System.Runtime.CompilerServices

Module Module1
    <Extension()> _
    Public Function DistanceFromThisPointTo( _
       ByVal PointA As Point, ByVal PointB As Point) As Single
        Return Math.Sqrt( _
           (PointA.X - PointB.X) ^ 2 + _
           (PointA.Y - PointB.Y) ^ 2)
    End Function
End Module
Here, you add the <Extension> attribute to the extension method (DistanceFromThisPointTo()) and the first parameter of the extension method (which is of type Point) indicates to the compiler that this extension method must be added to the Point class. The rest of the parameter list is then the signature of the extension method. To use the extension method, simply call it from a Point object, like this:

        Dim ptA As New Point(3, 4)
        Dim ptB As New Point(5, 6)
Extension methods can be applied to the following types:
  • Classes (reference types)
  • Structures (value types)
  • Interfaces
  • Delegates
  • ByRef and ByVal arguments
  • Generic method parameters
  • Arrays
Partial Methods
In VB 8.0, you had the concept of partial classes, in which the definition of a class could be split into more than one class definition. In VB 9.0, this concept is extended to methods–partial methods. To see how partial methods work, consider the following example: suppose you have a partial Contact class that contains two properties: Name and Email (see Listing 1).

Now, further suppose you want to allow the users of this partial class to optionally log the email address of each contact when its Email property is set. In this case, you can define a partial method called LogEmail(), as shown in Listing 2.

In this example, you've defined a partial method named LogEmail() that is called when a contact's email is set via the Email property. Observe that this method has no implementation. So where is the implementation of the LogEmail() partial method? It can optionally be implemented in another partial class. For example, if another developer decides to use the Contact partial class, s/he can define another partial class containing the implementation for the LogEmail() method:

Partial Public Class Contact

    Private Sub LogEmail()
    End Sub

End Class
So now, when you instantiate an instance of the Contact class, you can set its Email property as follows and a line will be printed in the output window:

        Dim Contact1 As New Contact
        Contact1.Email = "weimenglee@learn2develop.net" 
What if there is no implementation of the LogEmail() method? In that case, the compiler simply removes the call to this method and there's no change to your code.

Partial methods are useful when you're dealing with generated code. For example, suppose the Contact class is generated by a code generator. The signature of the partial method is defined in the class, but the implementation is totally up to you to decide if you need to use it.

When defining partial methods, note the following conditions:

  • The method must be a Sub, not a Function
  • The body of the method must be left empty
  • The access modifier must be Private

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date