Geeks With Blogs
Colin Bowern ... more of the usual bool

I love the new TryParse methods in the .NET Framework 2.0.  It's too bad that not all types have a TryParse method.  There was a piece of feedback posted on MSDN about implementing one for Guid values.  Here's one I threw together for Guid values.  Feedback always appreciated:

 

/// <summary>
/// Converts the string representation of a Guid to its Guid 
/// equivalent. A return value indicates whether the operation 
/// succeeded. 
/// </summary>
/// <param name="s">A string containing a Guid to convert.</param>
/// <param name="result">
/// When this method returns, contains the Guid value equivalent to 
/// the Guid contained in <paramref name="s"/>, if the conversion 
/// succeeded, or <see cref="Guid.Empty"/> if the conversion failed. 
/// The conversion fails if the <paramref name="s"/> parameter is a 
/// <see langword="null" /> reference (<see langword="Nothing" /> in 
/// Visual Basic), or is not of the correct format. 
/// </param>
/// <value>
/// <see langword="true" /> if <paramref name="s"/> was converted 
/// successfully; otherwise, <see langword="false" />.
/// </value>
/// <exception cref="ArgumentNullException">
///        Thrown if <pararef name="s"/> is <see langword="null"/>.
/// </exception>
public static bool GuidTryParse(string s, out Guid result)
{
    if (s == null)
        throw new ArgumentNullException("s");
    Regex format = new Regex(
        "^[A-Fa-f0-9]{32}$|" + 
        "^({|\\()?[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(}|\\))?$|" +
        "^({)?[0xA-Fa-f0-9]{3,10}(, {0,1}[0xA-Fa-f0-9]{3,6}){2}, {0,1}({)([0xA-Fa-f0-9]{3,4}, {0,1}){7}[0xA-Fa-f0-9]{3,4}(}})$");
    Match match = format.Match(s);
    if (match.Success)
    {
        result = new Guid(s);
        return true;
    }
    else
    {
        result = Guid.Empty;
        return false;
    }
}
Posted on Wednesday, January 18, 2006 7:40 PM | Back to top


Comments on this post: Guid TryParse

# re: Guid TryParse
Requesting Gravatar...
Thanks for posting this. I took your implementation and made it a little more conscise (plus removed the throw so the caller can wrap the call in an if statement and throw as needed.)

public static bool GuidTryParse( string s, out Guid result ) {
result = Guid.Empty;
if ( string.IsNullOrEmpty( s ) || !( new Regex( _guidMatchPattern ) ).IsMatch( s ) ) { return false; }
result = new Guid( s );
return true;
}
Left by Chris Mohan on Apr 27, 2007 12:49 PM

# re: Guid TryParse
Requesting Gravatar...
Thank you for usefull example!
Left by Konstantin Frankov on Apr 13, 2010 6:50 AM

# re: Guid TryParse
Requesting Gravatar...
.NET 4.0 has arrived with Guid.TryParse. :-)
Left by Ashish Gupta on May 04, 2010 3:05 AM

# re: Guid TryParse
Requesting Gravatar...
your method says its GUID but Guid.TryParse says its not for a valid guid
eg:033FBDD0-9850-49D0-82B2-D11B618FD773

Seems your method is more accurate or am i missing something?
Left by ram on Jun 12, 2012 5:16 AM

Your comment:
 (will show your gravatar)


Copyright © Colin Bowern | Powered by: GeeksWithBlogs.net | Join free