PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[Solved] Check if string is Guid-VBForums
Results 1 to 3 of 3

Thread: [Solved] Check if string is Guid

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    5

    [Solved] Check if string is Guid

    In my project I am setting a Guid variable from a String.

    VB Code:
    1. Dim myString as String
    2. Dim myGuid as Guid
    3. myGuid = new Guid(myString)
    Is there an easy approach to check if myString has a valid Guid format?
    I would rather not use the Regex function but if that is the only solution...

    Torbis
    Last edited by Torbis; Jan 16th, 2007 at 07:10 AM.

  2. #2
    Moderator.NET kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,383

    Re: Check if string is Guid

    the GUID(string) constructor can throw 3 possible exceptions. You can use this to see if the value passed in was valid or not. If it doesn't throw an exception, it was valid.

    VB Code:
    1. Dim myString As String = String.Empty
    2.             Dim myGuid As Guid = Nothing
    3.             Try
    4.                 myGuid = New Guid(myString)
    5.             Catch ex As FormatException
    6.                 'INVALID GUID WAS PASSED IN
    7.             Catch ex As OverflowException
    8.                 'INVALID GUID WAS PASSED IN
    9.             Catch ex As ArgumentNullException
    10.                 'INVALID GUID WAS PASSED IN
    11.             End Try
    of course if you didn't care which of the 3 possible exceptions were thrown, because all you want to know is if there was an error or not, you could simply just catch the base exception.

    VB Code:
    1. Dim myString As String = String.Empty
    2.             Dim myGuid As Guid = Nothing
    3.             Try
    4.                 myGuid = New Guid(myString)
    5.             Catch ex As Exception
    6.                 'INVALID GUID WAS PASSED IN
    7.             End Try
    Using VS.NET 2013/.NET 2.0 through 4.5 * Please mark you thread resolved using the Thread Tools above
    PLEASE INDICATE WHAT VERSION OF VB YOU USE!!!!!!!!!!!
    * If you found a post useful then please Rate it! * DO NOT PM ME WITH LINKS TO YOUR THREADS FOR ANSWERS PLEASE!

    Code Bank:Manipulate HTML Page content in the Web Browser Control from VB - Drag Drop from Windows into Win Form - Launch new default browser instance to open URL - Display Internet Image in Picturebox - Download Files From Web With Progress Bar - IP Textbox User Control - Installing .NET Framework with INNO Setup
    ZerosAndTheOne.com
    -=Matt=-

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    5

    Re: Check if string is Guid

    Thanks for reply.
    If anyone wants to use Regex for this:
    VB Code:
    1. Dim isGuid As Regex = New Regex("^(\{){0,1}[0-9a-fA-F]{8}\-" & _
    2.                      "[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-" & _
    3.                      "[0-9a-fA-F]{12}(\}){0,1}$", RegexOptions.Compiled)
    4. Dim myString as String = "something"
    5. Dim validString as String = "11111111-1111-1111-1111-111111111111"
    6. isGuid.IsMatch(myString) ' returns false
    7. isGuid.IsMatch(validString) ' returns true
    Torbis

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width