Erik Porter Saves Time

Published 26 April 04 04:06 PM | mhawley

Sounds like a nice headline for a newspaper, huh. I thought so too. Anyway, to the point of this post, I was struggling with formatting a Guid to the "registry" way of viewing it, you know - 32 digits separated by hyphens with curly brackets around it.

1st attempt - Code: String.Format("{{0}}", myguid)
1st attempt - Result: "{0}" <-- Which is very weird indeed, maybe a bug?

And then whammo! It dawned on me. I had just recently seen this post by Erik Porter...but I really didn't remember the meat of what Guid.ToString("N") meant (I hadn't pulled the post up yet).

2nd attempt - Code: myguid.ToString("N")
2nd attempt - Result: 23452345234235235242 (I know this isn't 32 digits)

Err - so that wasn't quite what I expected, but then again, I really wasn't sure what that "N" did. Ohh, so why not just open up the SDK docs, Microsoft had to have put something in there for the format I wanted. Ahh, sure enough, theres a "B" that gets what I want.

3rd attempt - Code myguid.ToString("B")
3rd attempt - Result: {2315123-2314-....}

Wahoo, Success! Man am I glad Erik saw & blogged about this the other week. Time saved, about 15 - 20 minutes wondering why the heck I'm getting {0} for the format of {{0}}. Thanks Erik!

PS - Whoever said blogs weren't a good source of information.

Filed under:

Comments

# HumanCompiler said on April 26, 2004 07:10 PM:

Glad to help, Matt! Thanks for pointing out "B" :) Now when I forget how to do it, I can look through my blog, click on the trackback and there your entry will be! :D

# Wayne Allen said on April 26, 2004 09:43 PM:

Don't forget our other friends N, D & P! I had to figure this out the hard way when dealing with the MSCRM SDK.

"N" 32 digits:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

"D" 32 digits separated by hyphens:
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx


"P" 32 digits separated by hyphens, enclosed in parentheses:
(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)

# Jerry Pisk said on April 27, 2004 01:38 AM:

Long live censorship :)

# Oskar Austegard said on April 27, 2004 10:05 AM:

If all you wanted in the first attempt was to put curly brackets around the guid, you should have done this {{{0}}}.

As the documentation states:
'The leading and trailing brace characters, '{' and '}', are required. To specify a single literal brace character in format, specify two leading or trailing brace characters; that is, "{{" or "}}".'
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemstringclassformattopic1.asp)

This Blog

News

.NET Links

Blogs I Read

Syndication