mike mcintyre's

.N e t J o u r n a l

vbCity Blogs moved to:
http://cs.vbcity.com/blogs
  Home :: Syndication  :: Login

AprMay 2013Jun
SMTWTFS
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

Archives

Topics

Source Code

Note: This article is based on Orcas Beta 2 (Visual Studio 2008).

 

Extension Methods

Visual Basic 2008 extension methods enable you to "add" functionality to existing types without creating a new derived type.

Types That Can Be Extended

· Classes (reference types)

· Structures (value types)

· Interfaces

· Delegates

· ByRef and ByVal arguments

· Generic method parameters

· Arrays

Extension methods make it possible to create a method in a Visual Basic module that can be called as if it were an instance method of another type.

An extension method can be a Sub procedure or a Function procedure. You cannot define an extension property, field, or event. All extension methods must be marked with the extension attribute from the System.Runtime.CompilerServices namespace.

The first parameter in an extension method definition specifies which data type the method extends. When the method is run, the first parameter is bound to the instance of the data type that invokes the method.

In the following example an extension method is defined for the .NET String type.

Example – Add ‘Display’ extension method to .NET String type

The following code defines a Display extension method for the .NET String data type. The method uses MessageBox.Display to display a string. The type of the aString parameter of the Display method, establishes that the method extends the String class.

 

' You must define type extensions in a Visual Basic module.

Module ExampleTypeExtensions

 

    'All extension methods must be marked with the extension attribute

    '  from the System.Runtime.CompilerServices namespace.

    _

    Public Sub Display(ByVal aString As String)

        MessageBox.Show(aString)

    End Sub

 

End Module

The following code, in a button click handler in a Windows form, calls the String ‘Display’ extension method defined above.

 

Public Class Form1

 

    Private Sub DisplayStringButton_Click _

      (ByVal sender As System.Object, ByVal e As System.EventArgs) _

      Handles DisplayStringButton.Click

 

        Dim name As String = "John Doe"

        name.Display()

 

    End Sub

End Class

 

Result

The name 'John Doe' is displayed in a message box. 

Notes

You can use extension methods to extend a type’s methods, but not to override them.

All that is required to be able to run extension methods is that they be in scope. If an extension method is in scope, it is visible in IntelliSense and can be called as if it were an ordinary instance method.

Notice that when the ‘Display’ extension method is invoked in example, no argument is sent in for the first parameter. Parameter aString in the extension method definition is automatically bound to the instance of String that calls them.

Because the first parameter specifies the data type that the extension method extends, it is required and cannot be optional. For that reason, Optional parameters and ParamArray parameters are excluded.

When an extension method has the same name as an existing method but a different signature, both methods are allowed as long as the signatures do not conflict.

When an extension method has the same name and signature as an existing method in the type being extended, resolve ambiguity by using the fully qualified name to specify the extension method you are calling. In the example the Display extension method is defined in a module named ExampleTypeExtensions, the fully qualified name is ExampleTypeExtensions.Display.

In general, it is recommended that you implement extension methods sparingly. When possible, client code that extends an existing type should do so by creating a new type derived from the existing type.

When using an extension method to extend a type whose source code you cannot change, you run the risk that a change in the implementation of the type will cause your extension method to break.

Mike McIntyre

http://www.getdotnetcode.com

posted on Friday, September 21, 2007 3:19 PM