Welcome Guest!
Create Account | Login
Locator+ Code:

FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Click here to receive your FREE subscription to Visual Studio Magazine

email article
printer friendly

Beautify Your Code With Extensions
Extension methods bring together old and new ways of working with data, and open the doors to new language opportunities.
by Bill McCarthy

May 25, 2007

Technology Toolbox: VB .NET

Extension methods are a new language feature that will become available with Visual Basic 9.0 (VB Orcas).


The great part about these methods: They give you enormous flexibility when working with objects of all types. Combined with LINQ, these methods give you the power to keep query statements simple, yet use powerful case-specific filtering techniques. I'll walk you through how to put these extension methods to work in your applications, explaining both their design and usage, as well as how they integrate into Language Integrated Query syntax (LINQ). Before I get into the specifics, however, let's review the historical and current capabilities of VB for context.

In VB6, you can use the Len function if you want to know the length of a string. The Len function is a Shared method in the VBA.Strings function library. When ported to the .NET platform, this functionality was moved into the Microsoft.VisualBasic.Strings function library, and it remains essentially the same: a Shared method that returns the length of a string or 0 for null references.

With the introduction of .NET, Strings became objects, not just data holders. As such, the String type now comes with methods as part of the object instance, including a Length property.

The old model used function libraries; the new model uses instance methods. Both have advantages and disadvantages when compared to the other. Function libraries allow you to use different methods based on how you want to use the data; this is similar to how banks process checks differently, depending on their needs and practices. Function libraries also allow you to deal with null references gracefully. On the other hand, the object-orientated approach offers great discoverability through the "dot" syntax, as well as providing the basis for run-time polymorphic behavior in non-sealed (inheritable) types.

Extension methods are static (Shared) methods, which are essentially the same thing as function libraries, but with one important twist: Extension methods appear through the dot syntax on variables of the type of the first parameter (or derived from it). The dot-syntax behavior of an extension method makes it appear as if it is an extension of another type. This ability to "extend" other types comes in handy when you need to add behavior to NotInheritable (sealed) classes such as String. For example, you can create an extension method that returns a Nullable(Of Int32) for a String's length:

Public Module MyExtensions

<Extension()> _
	Public Function NullableLength( _
	ByVal value As String) _
	As Nullable(Of Int32)
	If value Is Nothing Then
		Return Nothing
		Return value.Length
	End If
End Function

End Module

You use this extension method by calling it as you would an instance method; the instance is passed automatically to the method's first parameter. The extension method appears in Intelli-Sense for the String type (Figure 1).

Back to top

Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTPOnline Home