Search

Guid.TryParse by Erik Porter

Closed
as Fixed Help for as Fixed

36
0
Sign in
to vote
Type: Suggestion
ID: 94072
Opened: 7/11/2004 12:54:54 PM
Access Restriction: Public
2
Workaround(s)
No Guid.TryParse Method
Details (expand)
Product Language
English
Version
Visual Studio 2005 Beta 1
Category
Other
Operating System
Windows XP Professional
Operating System Language
US English
Proposed Solution
Add a Method called TryParse to the Guid Class that takes a String and has a result parameter passed in by reference
Benefits
Faster Development
Other Benefits
Faster Development
Sign in to post a comment.
Posted by Microsoft on 10/22/2009 at 10:35 PM
Guid.TryParse and Enum.TryParse<T> are both in .NET 4 Beta 2. Hope this helps!

Regards,

Justin Van Patten
Program Manager
CLR Base Class Libraries
Posted by Evgeny0 on 10/2/2009 at 12:02 AM
It doesn't seem to be in Beta 1.
Posted by Marc C Brooks on 6/23/2009 at 9:46 PM
Now if we could just get an Enum.TryParse, most of my spurious exception catch swallowing would disappear.
Posted by Microsoft on 6/23/2009 at 4:28 AM
Hi Erik Porter,

I apologize for the long delay; the nature of our releases over the past number of years has made it difficult for us to add such improvements. The good news is that we've finally been able to address this; we've added Guid.TryParse to .NET 4. It should be available in the next public preview release.

Regards,

Justin Van Patten
Program Manager
Common Language Runtime, Base Class Libraries
Posted by R Clarke on 4/25/2008 at 6:14 PM
Shouldn't the work around be more properly written with the Regex as a static field outside of the static method instead of creating a new regex every time the static method is called?
Posted by Microsoft on 7/16/2004 at 3:21 PM
track devdiv schedule - see links tab
Posted by Microsoft on 7/16/2004 at 12:05 PM
Please use parent bug for tracking this issue.
Posted by Microsoft on 7/15/2004 at 10:50 AM
Thanks for the suggestion, we'll add this as a consideration for a future release!
Posted by Microsoft on 7/13/2004 at 11:45 AM
tracking as a feature request
Posted by Microsoft on 7/13/2004 at 11:41 AM
tracking as a feature request
Sign in to post a workaround.
Posted by schub on 6/8/2007 at 2:09 PM
// i think it is easier the following way: (just the bare minimum, build your specific needs around it...)

try {
gcnew Guid(newGuidString);
}
catch (Exception^ e) {
return false;
}
return true;
Posted by Erik Porter on 1/18/2006 at 2:43 PM
/// <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;
    }
}

---

Test values:

Guid Result, ResultValue;
bool IsValid = false;

// dddddddddddddddddddddddddddddddd
IsValid = ArgumentValidation.GuidTryParse("ca761232ed4211cebacd00aa0057b223", out Result);
Assert.IsTrue(IsValid);
ResultValue = new Guid("ca761232ed4211cebacd00aa0057b223");
Assert.AreEqual<Guid>(Result, ResultValue);

// dddddddd-dddd-dddd-dddd-dddddddddddd
IsValid = ArgumentValidation.GuidTryParse("CA761232-ED42-11CE-BACD-00AA0057B223", out Result);
Assert.IsTrue(IsValid);
ResultValue = new Guid("CA761232-ED42-11CE-BACD-00AA0057B223");
Assert.AreEqual<Guid>(Result, ResultValue);

// {dddddddd-dddd-dddd-dddd-dddddddddddd}
IsValid = ArgumentValidation.GuidTryParse("{CA761232-ED42-11CE-BACD-00AA0057B223}", out Result);
Assert.IsTrue(IsValid);
ResultValue = new Guid("{CA761232-ED42-11CE-BACD-00AA0057B223}");
Assert.AreEqual<Guid>(Result, ResultValue);

// (dddddddd-dddd-dddd-dddd-dddddddddddd)
IsValid = ArgumentValidation.GuidTryParse("(CA761232-ED42-11CE-BACD-00AA0057B223)", out Result);
Assert.IsTrue(IsValid);
ResultValue = new Guid("(CA761232-ED42-11CE-BACD-00AA0057B223)");
Assert.AreEqual<Guid>(Result, ResultValue);

// {0xdddddddd, 0xdddd, 0xdddd,{0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd}}
IsValid = ArgumentValidation.GuidTryParse("{0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}}", out Result);
Assert.IsTrue(IsValid);
ResultValue = new Guid("{0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}}");
Assert.AreEqual<Guid>(Result, ResultValue);

IsValid = ArgumentValidation.GuidTryParse("{0xA,0xA,0xA,{0xA,0xA,0xA,0xA,0xA,0xA,0xA,0xA}}", out Result);
Assert.IsTrue(IsValid);
ResultValue = new Guid("{0xA,0xA,0xA,{0xA,0xA,0xA,0xA,0xA,0xA,0xA,0xA}}");
Assert.AreEqual<Guid>(Result, ResultValue);

See above code.