locked
How do I convert a string to a uniqueidentifier? RRS feed

  • Question

  • User-858993849 posted

    if I have stored a  uniqueidentifier in a string, how do I convert it back to a uniqueidentifier?

    Monday, August 11, 2014 9:22 AM

Answers

  • User-1360095595 posted

    Guid g = new Guid (yourString);

    Add long as your string is a valid Guid. 

    Edit: and here's a reference link: http://msdn.microsoft.com/en-us/library/system.guid(v=vs.110).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 11, 2014 9:32 AM
  • User281315223 posted

    You can either pass your string into a Guid constructor or use the Guid.Parse() method to handle this conversion (assuming that you have a string that is properly formatted as a GUID or uniqueidentifier) :

    // Option 1: Using the Guid constructor
    Guid uniqueIdentifier = new Guid(yourUniqueIdentifierString);
    
    // Option 2: Using the Guid.Parse() method
    Guid uniqueIdentifier = Guid.Parse(yourUniqueIdentifierString);

    As discussed in this Stack Overflow thread, both of these approaches are equivalent for all intents and purposes :

    public Guid(string g)
    {
        if (g == null)
        {
           throw new ArgumentNullException("g");
        }
        this = Empty;
        GuidResult result = new GuidResult();
        result.Init(GuidParseThrowStyle.All);
        if (!TryParseGuid(g, GuidStyles.Any, ref result))
        {
            throw result.GetGuidParseException();
        }
        this = result.parsedGuid;
    }
    
    public static Guid Parse(string input)
    {
        if (input == null)
        {
            throw new ArgumentNullException("input");
        }
        GuidResult result = new GuidResult();
        result.Init(GuidParseThrowStyle.AllButOverflow);
        if (!TryParseGuid(input, GuidStyles.Any, ref result))
        {
            throw result.GetGuidParseException();
        }
        return result.parsedGuid;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 11, 2014 9:39 AM

All replies

  • User-1360095595 posted

    Guid g = new Guid (yourString);

    Add long as your string is a valid Guid. 

    Edit: and here's a reference link: http://msdn.microsoft.com/en-us/library/system.guid(v=vs.110).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 11, 2014 9:32 AM
  • User281315223 posted

    You can either pass your string into a Guid constructor or use the Guid.Parse() method to handle this conversion (assuming that you have a string that is properly formatted as a GUID or uniqueidentifier) :

    // Option 1: Using the Guid constructor
    Guid uniqueIdentifier = new Guid(yourUniqueIdentifierString);
    
    // Option 2: Using the Guid.Parse() method
    Guid uniqueIdentifier = Guid.Parse(yourUniqueIdentifierString);

    As discussed in this Stack Overflow thread, both of these approaches are equivalent for all intents and purposes :

    public Guid(string g)
    {
        if (g == null)
        {
           throw new ArgumentNullException("g");
        }
        this = Empty;
        GuidResult result = new GuidResult();
        result.Init(GuidParseThrowStyle.All);
        if (!TryParseGuid(g, GuidStyles.Any, ref result))
        {
            throw result.GetGuidParseException();
        }
        this = result.parsedGuid;
    }
    
    public static Guid Parse(string input)
    {
        if (input == null)
        {
            throw new ArgumentNullException("input");
        }
        GuidResult result = new GuidResult();
        result.Init(GuidParseThrowStyle.AllButOverflow);
        if (!TryParseGuid(input, GuidStyles.Any, ref result))
        {
            throw result.GetGuidParseException();
        }
        return result.parsedGuid;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 11, 2014 9:39 AM
  • User-858993849 posted

    Thanks guys.  Your the best

    Monday, August 11, 2014 10:04 AM