Locations of visitors to this page


VB 2008 Extension Methods

Onteora Software

Ken Tucker's Blog

About the author

Author Name is someone.
E-mail me Send mail

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2009

VB 2008 Extension Methods

VB 2008 Extension Methods


Visual Basic 2008 adds a new features called extension methods.   These allow you to add a method, or function to a type.  For this example we will add an IsGuid function to strings.  All extension methods must be placed in a module.  The function or method must be marked as an extension and the first argument is the type the method extends.



Imports System.Runtime.CompilerServices
Imports System.Text.RegularExpressions

Module Module1

    Sub Main()
        Dim g As String = "82ee4145-632c-42a1-83b9-57ec163eaa17"
        Dim g1 As String = "82ee4145-632c-42a1-83b9-57ec163ea17"

        Console.WriteLine(g & IIf(g.IsGuid, " is ", " is not ") & "a valid guid")
        Console.WriteLine(g1 & IIf(g1.IsGuid, " is ", " is not ") & "a valid guid")
    End Sub

End Module

Public Module MyExtensions

    <Extension()> _
    Public Function IsGuid(ByVal s As String) As Boolean
        Dim regGuid As New Regex("^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$", RegexOptions.Compiled)
        Return regGuid.IsMatch(s)
    End Function
End Module

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: VS 2008
Posted by Ken Tucker on Friday, June 29, 2007 10:12 PM
Permalink | Comments (2) | Post RSSRSS comment feed

Related posts

Comments

Busby SEO Test us

Wednesday, December 10, 2008 5:44 PM

Busby SEO Test

thanks for sharing your post and your ideas!!i really enjoy it thanks and more power!!

Busby SEO Test Pinay us

Thursday, December 25, 2008 1:14 AM

Busby SEO Test Pinay

the code is great i can use it as my reference

Comments are closed