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
    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
  • 1
    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. – Kev Aug 25 '11 at 22:33
  • 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. – JustBeingHelpful Aug 25 '11 at 22:35
  • The edit history on BrokenGlasses answer shows that you edited in Kiley's answer. – Kev Aug 25 '11 at 22:38
  • 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. – JustBeingHelpful Aug 25 '11 at 22:40
  • Nope, I edited Broken Glass' answer. Could there be a bug with Stack Overflow? I never edited Kiley's. – JustBeingHelpful Aug 25 '11 at 22:41
  • Yes but you copied in [Kyleys answer](http://stackoverflow.com/questions/7197790/cannot-convert-string-to-guid-in-c-net/7197808#7197808) 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. – Kev Aug 25 '11 at 22:45
  • Check the edit history: http://stackoverflow.com/posts/7197810/revisions – Kev Aug 25 '11 at 22:46
  • 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?? – JustBeingHelpful Aug 25 '11 at 22:46
  • Do not edit other peoples answers unless it is to make minor corrections. For example, here: http://stackoverflow.com/posts/6900815/revisions you should have edited that into your question as an update and referenced the answer. Do not do this. – Kev Aug 25 '11 at 22:50
  • 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? – JustBeingHelpful Aug 25 '11 at 22:50
  • @Mr.MacGyver let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2873/discussion-between-kev--and-mr-macgyver) – Kev Aug 25 '11 at 22:51
  • 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. – JustBeingHelpful Aug 25 '11 at 22:57
  • 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. – JustBeingHelpful Aug 25 '11 at 22:58
  • So who edited what? – Cloud Nov 01 '17 at 11:17

6 Answers6

65

Try this:

Guid ownerIdGuid = Guid.Empty;            
try
{
    string ownerId = CallContextData.Current.Principal.Identity.UserId.ToString();
    ownerIdGuid = new Guid(ownerId);
}
catch
{
    // implement catch 
}
Kiley Naro
  • 1,749
  • 1
  • 14
  • 20
  • cannot implicitly convert type 'string' to 'System.Guid' – JustBeingHelpful Aug 25 '11 at 22:09
  • I didn't see your edit in time. :-) But gave you +1 anyways – JustBeingHelpful Aug 25 '11 at 22:24
  • 1
    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. :( – Kiley Naro Aug 25 '11 at 22:27
  • Yes you're right.. I didn't even realize there was an "oldest" tab in Stack Overflow.. looks like you beat him. – JustBeingHelpful Aug 25 '11 at 22:30
  • @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" – BrokenGlass Aug 26 '11 at 00:07
  • @BrokenGlass 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! – Kiley Naro Aug 26 '11 at 17:11
  • 1
    @Kiley: no worries and welcome to SO, this wasn't a big thing anyway – BrokenGlass Aug 26 '11 at 21:19
25

Try this:

ownerIdGuid = Guid.Parse(ownerId);

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

Kev
  • 118,037
  • 53
  • 300
  • 385
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
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)
Chris Schmich
  • 29,128
  • 5
  • 77
  • 94
5

Try one of these:

Guid.Parse
Guid.TryParse
Gruid.TryParseExact

in .NET 4.0 (or 3.5)

juFo
  • 17,849
  • 10
  • 105
  • 142
  • 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. – JustBeingHelpful Apr 17 '12 at 16:54
2

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

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
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)

JRrelyea
  • 139
  • 1
  • 4