Cannot convert string to GUID in C#.NET [duplicate]
Asked Answered
D

6

42

Why would the cast (to a System.Guid type) statement be invalid (second line in try block)?

For example, suppose I have a string with a value of "5DD52908-34FF-44F8-99B9-0038AFEFDB81". I'd like to convert that to a GUID. Is that not possible?

    Guid ownerIdGuid = Guid.Empty;
    try
    {
        string ownerId = CallContextData.Current.Principal.Identity.UserId.ToString();
        ownerIdGuid = (Guid)ownerId;
    }
    catch
    {
        // Implement catch
    }
Demonstrable answered 25/8, 2011 at 22:4 Comment(14)
Please do not cut and paste other user's answers into other user's posts. Just mark as accepted and optionally upvote. That is all that's required.Seduction
I did not. I merely took the answer from this comment: @Mr. MacGyver: Guid.Parse() is .NET 4, you should be able to do ownerIdGuid = new Guid(ownerId) in .NET 2.0 ..... it just so happens that Kiley answered almost the same time that comment was made. So it was mere coincidence... I had the exact same code in Visual Studio, so it appeared that way.Demonstrable
The edit history on BrokenGlasses answer shows that you edited in Kiley's answer.Seduction
Also, when I first came to this site, I didn't look for answers or expect answers in the comments, so I always like to see the real answer in the body of the answered answer. So that is the reason I edited Broken Glass' answer with the actual answer from his comment.Demonstrable
Nope, I edited Broken Glass' answer. Could there be a bug with Stack Overflow? I never edited Kiley's.Demonstrable
Yes but you copied in Kyleys answer to BrokenGlass's answer. Also you do not need to add (in large bold letters) Mr. MacGyver's Edit:. Also ".ToString() and Guid.Parse didn't work." isn't adding to the answer, leave that in the comments where it belongs.Seduction
Check the edit history: stackoverflow.com/posts/7197810/revisionsSeduction
There could very well be a bug... After I edited Broken Glass' answer, I marked his as the answer. Then went down, and read Kiley's comment, then clicked the "oldest" tab. Then saw that Kiley's was the oldest and marked his as the answer. Then added a comment. See if you can reproduce it. Perhaps the switching between the tabs mixed up the index of the what answer I edited??Demonstrable
Do not edit other peoples answers unless it is to make minor corrections. For example, here: stackoverflow.com/posts/6900815/revisions you should have edited that into your question as an update and referenced the answer. Do not do this.Seduction
Kev, are you showing that as me editing Broken Glass' answer or Kiley's answer? Those were the edits I made to Broken Glass' answer. Is it showing different for you?Demonstrable
@Mr.MacGyver let us continue this discussion in chatSeduction
I got this from Broken Glass and Kiley's original answer. ".ToString() and Guid.Parse". But for the record, I did not copy anything from Kiley's into Broken Glass. ..apart from programmers being the most stubborn people on the planet, I will take your advice in future posts (including myself). And I am a newbie to this site. I will not edit answers anymore. But one suggestion for the site... for novice programmers searching google for answers, they will not see the actual answer in a comment.Demonstrable
That's the main reason I edited Broken Glass' answer. To make it clear to a reader on the internet what the actual answer is. Since I changed my answer to Kiley's answer, everything is all good.Demonstrable
So who edited what?Nuristan
E
66

Try this:

Guid ownerIdGuid = Guid.Empty;            
try
{
    string ownerId = CallContextData.Current.Principal.Identity.UserId.ToString();
    ownerIdGuid = new Guid(ownerId);
}
catch
{
    // implement catch 
}
Edwyna answered 25/8, 2011 at 22:6 Comment(7)
cannot implicitly convert type 'string' to 'System.Guid'Demonstrable
I didn't see your edit in time. :-) But gave you +1 anywaysDemonstrable
I'm a bit new to SO so I might not understand quite how all of this works, but you took my answer and cut+pasted it into someone else's answer and then marked that as the correct answer... I appreciate the upvote but that seems a bit unfair. :(Edwyna
Yes you're right.. I didn't even realize there was an "oldest" tab in Stack Overflow.. looks like you beat him.Demonstrable
@Kiley: This would be ok if your answer had been correct first - you only edited in a working version 4 minutes later. I don't see how this is "unfair"Encrata
@Encrata Sorry for all of the trouble with these comments and edits and who gets the check mark and who doesn't. Both of our answers are eventually correct, around the same time, so I'll give you an upvote. Yours works for .NET 4.0, mine works for .NET 3.5. Sorry again for the trouble on all of this!Edwyna
@Kiley: no worries and welcome to SO, this wasn't a big thing anywayEncrata
E
25

Try this:

ownerIdGuid = Guid.Parse(ownerId);

ownerId is a string, you cannot cast it to a Guid directly.

Encrata answered 25/8, 2011 at 22:6 Comment(6)
System.Guid does not contain a definition for parseDemonstrable
@Mr. MacGyver: What .NET version are you working with?Encrata
Http://msdn.microsoft.com/en-us/library/system.guid.parse.aspx Converts the string representation of a GUID to the equivalent Guid structure.Trichome
@Mr. MacGyver: Guid.Parse() is .NET 4, you should be able to do ownerIdGuid = new Guid(ownerId) in .NET 2.0Encrata
I see it's only available on 4.0.. I wonder why .ToString() doesn't work?Demonstrable
Kiley answered first. Have to play by the rules. I'm MacGyver after all.Demonstrable
U
7

You cannot cast directly from string to Guid. Instead, use either:

  1. Guid.Parse (throws FormatException on invalid format); or
  2. Guid.TryParse (returns false on invalid format)
Unglue answered 25/8, 2011 at 22:7 Comment(1)
this only works with .NET 4.0, but +1 for the answerDemonstrable
I
5

Try one of these:

Guid.Parse
Guid.TryParse
Gruid.TryParseExact

in .NET 4.0 (or 3.5)

Idell answered 17/4, 2012 at 15:41 Comment(1)
Regarding your comment "in .NET 4.0 (or 3.5)", .NET 3.5 requires you to instantiate a Guid first (per the answer by @Kiley). I just added a tag of ".NET-3.5" to my question so it's clear to the reader. Your answers work fine, but only in the .NET 4.0 framework. Also, would the "Gruid.TryParseExact", be "Guid.TryParseExact"? I wasn't aware of that one.. thanks for posting.Demonstrable
B
2

You need to use Guid.Parse to convert from string to Guid

Bunton answered 25/8, 2011 at 22:7 Comment(2)
I commented with the error I got with this belowDemonstrable
+1 for .NET framework version 4.0Demonstrable
M
0

System.Guid x = new System.Guid("5DD52908-34FF-44F8-99B9-0038AFEFDB81") works and answers what's being asked

(I know this is an old post)

Maize answered 4/11, 2017 at 22:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.