Adblock breaks this site

How to use extension methods in VB 2008

Discussion in 'Programming General' started by Flaming Idiots, Jul 7, 2008.

  1. Flaming Idiots

    Flaming Idiots Active Member
    Visual Basic Programmers

    Joined:
    Dec 22, 2005
    Posts:
    235
    Referrals:
    1
    Sythe Gold:
    0
    Two Factor Authentication User
    How to use extension methods in VB 2008

    Extension methods are a simple way to extend existing classes without having to create a new class. They help make your code more readable because, instread of writing code like this:

    Extensions.Print( Enumerable.Where(Process.GetProcesses, Function(X As Process) X.MainWindowTitle.Length > 0), Function(X As Process) X.MainWindowTitle)

    You can write it like this:

    Process.GetProcesses.Where(Function(X) X.MainWindowTitle.Length > 0).Print(Function(X) X.MainWindowTitle)

    VB 2008 already has several extension methods like Where, Select, OrderBy, ToArray and more. They are methods that have an arrow pointing down in the function list.
    [​IMG]

    To make an extension method, what you need to do is write a function in a module, make the first argument what you want it to extend and add the extension attribute to it. Here is an example:
    Code:
    Imports Extension = System.Runtime.CompilerServices.ExtensionAttribute
    
    Public Module Extensions
    
        <Extension()> _
        Public Sub Print(Of T)(ByVal Source As IEnumerable(Of T), Optional ByVal Selector As Func(Of T, Object) = Nothing)
            If Source Is Nothing Then Throw New ArgumentNullException("Source")
            If Selector Is Nothing Then Selector = Function(X) X
            For Each Item In Source
                Console.WriteLine(Selector(Item))
            Next
        End Sub
    
    End Module
    
    The Print extension method extends all IEnumberable(Of T) items, like arrays or anything you can use for a For Each loop.

    A good use for extension methods is for making parsing string easier. Here is one that will find all matches in a string:
    Code:
    <Extension()> _
    Public Function ParseString(ByVal Source As String, ByVal Pattern As String, _
                                Optional ByVal Options As System.Text.RegularExpressions.RegexOptions = _
                                System.Text.RegularExpressions.RegexOptions.None) _
                                As IEnumerable(Of System.Text.RegularExpressions.Match)
        Return System.Text.RegularExpressions.Regex.Matches(Source, Pattern, Options) _
               .OfType(Of System.Text.RegularExpressions.Match)()
    End Function
    
    You could use it for parsing out all RS worlds if you wanted to:
    Code:
    Dim Html = New System.Net.WebClient().DownloadString("http://www.runescape.com/slj.ws?order=WPAML")
    Dim Matches = Html.ParseString("e\((\d+),\d,\d+,""[a-zA-Z1-9\-\s]+"",\w+,""\w+"",(\d+),\d+\);")
    Matches.Print(Function(X) String.Format("World {0} has {1} players.", X.Groups(1).Value, X.Groups(2).Value))
    
    If you want more detailed information about extension methods, check this link http://msdn.microsoft.com/en-us/magazine/cc163317.aspx.
     
< How do I make a msg box come up on an error? | Help with a simple code >


 
 
Adblock breaks this site