/Home /Archive /Syndicate /Blog /Support /About /Contact  
All Visual Basic Feeds in one place!





In this article we will take a look at Extension Methods.
 
We will learn how they work internally with the compiler, how to create them and how to use them.
 
 
So what exactly is an Extension Method?
  
Before the time of Extension Methods if you wanted to extend the functionality of an existing class, you had to either add the new code to the existing class and recompile, or create a new derived class and compile it.
 
If you didn’t have the source code, well, then you were stuck using decompilers or just rewriting it from scratch.
 
Now that Extension Methods are here we can easily add new functionality to an existing type without the need for derived types, recompilation of original source, or the original source code at all.
 
 

Let us take a look at some of the important details.

  • As the name implies Extension Methods can only be a Sub Routine or Function.
    (That means we cannot create extension properties, fields, or events.)
  • Extensions must be decorated with the <Extension> attribute.
  • The first parameter specifies which data type the Extension Method or Function will extend. 


Start Building your own Extension Methods!

Now that we have some background information and a basic understanding of Extension Methods let us get started building our own.

Start by creating a new Windows Application called “ExtensionMethodDemo”.



After the application has been created you can begin by adding a New Module to your Project called “Extensions” or something of the sort.



After the Module has been added the first thing we must do is Import the System.Runtime.CompilerServices Namespace.

Add the following line to the top of your Module.

Imports System.Runtime.CompilerServices


After you have Imported the Compiler Services Namespace, we need to change the scope of the Module to Public.

Our Module should now look like this.

Imports System.Runtime.CompilerServices
Public Module Extensions

End Module

Now that we have our Module setup we can begin building our Extension Methods.

In this example we will be Extending the Form class to add a simple Docking method.

The Docking Method will move the specified form to the Bottom Right of the users Primary Screen.

Note: You will receive the following error if you try to type the XML Comments in first, those must be added after the method signature is created.
         “XML documentation comments must precede member or type declarations.”

Imports System.Runtime.CompilerServices
Public Module Extensions

    ''' <summary>
    ''' Docks the specified form to the Bottom Right of the Primary Screen
    ''' </summary>
    <Extension()> _
    Public Sub DockBottomRight(ByVal form As Form)
        form.Left = Screen.PrimaryScreen.WorkingArea.Width - form.Width
        form.Top = Screen.PrimaryScreen.WorkingArea.Height - form.Height
    End Sub

End Module

Now that our Extension Method is built, its time to test it.

Open the default form of the Windows Application that you created and inside the Form_Load event add the following code.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DockBottomRight()
    End Sub

End Class

If you hand typed this in you will notice the Extension Method now shows up in the Intellisense as shown below.

Now run the project, and check the outcome.

  
Sounds good, but how do they work?
 
You have built your own Extension Method now and have a basic understanding of them so now we will take a look at how they work on a deeper level.
  
Truth be told there really isn’t a bunch of magic going on behind the scenes, its quite simple.
 
Consider the following example.

Imports System.Runtime.CompilerServices
Public Module Extensions

    <Extension()> _
    Sub Print(ByVal s As String)
        Console.WriteLine(s)
    End Sub

End Module

With the Extension Attribute decorating the Print method, when the compiler finds a place in your code where you try to call a Print method of a String Class

Dim s as String = "Extending the Extendable"
s.Print()

it translates that call into this.

Dim s as String = "Extending the Extendable"
Extensions.Print(s)

As you can see, the compiler automatically searches for the Extension Method named Print and implements it for you.

  

Conclusion

By now you should have a fairly good understanding of Extension Methods, how they are created and used, as well as how they work internally with the compiler.

dp.SyntaxHighlighter.ClipboardSwf = '/dp.SyntaxHighlighter/Scripts/clipboard.swf' dp.SyntaxHighlighter.HighlightAll('eb67b169099a421bb200da05ee894f75') dp.SyntaxHighlighter.HighlightAll('069176f5dc1e41418033939035e7890b') dp.SyntaxHighlighter.HighlightAll('331d7c52f2e744dbb5092953a7c09dfd') dp.SyntaxHighlighter.HighlightAll('2971ce61c52f4f798ca39da8e3f516da') dp.SyntaxHighlighter.HighlightAll('a4f564a442b94e4dbca4d4e637afdfeb') dp.SyntaxHighlighter.HighlightAll('1b55aaab49124dbcbe335cdf5936669a') dp.SyntaxHighlighter.HighlightAll('6f0cef8988d8463897eac76dab7a1197')
© 2005 Serge Baranovsky. All rights reserved.
All feed content is property of original publisher. Designated trademarks and brands are the property of their respective owners.

This site is maintained by SubMain(), a division of vbCity.com, LLC