VB.NET IsGuid Extension Method

by Spyder July 08, 2009 23:36

Extension method to validate a string and check if it is a valid guid. May not be the fastest method in the world but it works. All comments and suggestions for improvements are welcome.

''' <summary>
''' String Extender to validate of the contents of
''' a string is an actual guid.
''' </summary>
''' <param name="value">String representation of a guid</param>
''' <returns>True if the string can be converted to a guid, false if not.</returns>
''' <remarks></remarks>
<Extension()> _
Public Function IsGuid(ByVal value As String) As Boolean

    ' Do not allow empty guids and skip empty strings

    If String.IsNullOrEmpty(value) Or value = Guid.Empty.ToString Then
        Return False
    End If
   

    ' try the convertion

    Try
        Dim id As Guid = New Guid(value)
        Catch ex As Exception
            Return False
    End Try

    ' We made it here we have a valid guid

    Return True

End Function

Check 'ToGuid' as an example of how this works.

Tags: ,

Programming

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen | Modified by Mooglegiant

About the author

I'm a Scott living in Canada.   My main interests are pool, fishing and windsurfing.  I have four kids and a wife to go with them.  My software background goes something like this. Assembler,  Basic, C, RPG  COBOL, C++, VB, SQL, HTML JAVA VB.NET, C#, XML etc, etc etc etc.

My main languages at the moment are:  C#, Ajax, ASP.NET 3.0/3.5, Powershell, Java and XML. I also dabble in F# but that’s still in it’s infancy. I am currently working on Flex and Grails development, as this is new technology to me I need to update this blog.