Gray's Matter
Justice Gray - North America's favorite metrosexual software consultant

I Wish These People Updated More Than Once a Year


I thought I would continue my seeming tradition of hitting these questions once every 6 months.   Actually, I had given up on Granny getting into Corillian but that's okay; she's seen the light and is now an independent consultant!  Nonetheless, I have still felt the need to answer some of Scott Hanselman's old interview questions in an attempt to help get her up to speed.

So, without further adieu, let's tell two stories about marriages:

Story #1:

My sister-in-law is getting married this coming May.  I had a chance to meet her future husband's parents this past Christmas, both of whom are from Quebec and primarily speak French.  As is my custom when I am meeting new people, I tried to amp up the patented Justice Gray charm ™ and impress them with the French I vaguely remembered from about a decade and a half ago.  I introduced myself ("Je m'appelle Justice") and then decided to follow up with the coup de grace: terrifically witty banter.  

"The French I am speaking now is the only French I know!"

I paused for laughter - but instead was met with nervous silence!!  In this moment, I finally learned what Donald goes through at the beginning of every EDMUG meeting.

I was definitely a little confused; after all that sentence should have been a guaranteed laugh riot.  My future brother-in-law came up to me and quietly whispered,
"I think I understood what you were saying, but my parents actually think you have some sort of handicap now"

It seems instead of my intended phrase, I had stated something closer to

"I speak French.  French speak!!  Only French!!  I know speak!!!"

Fortunately, I solved the awkwardness that night like anyone would: by getting *completely hammered*.

Speaking of solving all of the world's problems through excessive alcohol consumption , let's go onto story #2.

As most of you know, I was married a year and a half ago.  What many of you don't know is that one of my groomsmen, flush with alcohol and ready to try and impress the ladies, decided to go to one of the bridesmaids and show off his *fluent* knowledge of Cantonese.  Whatever his planned come-on line was will forever remain a mystery.  I wish I could say the same for the translation of what he *did* say, which was, "Boy, I'd really like to sleep with your mother."  

So, what's the problem here?  Lost or unclear intent through translation, or lack thereof.  This is also the difficulty Scott Hanselman was referring to when he asked

"What’s wrong with a line like this? DateTime.Parse(myString);"

This line on its own seems pretty simple - after all, you're just asking for the runtime to take a standard string and convert it into a DateTime object.  

DateTime myDate = DateTime.Parse("10/06/2006");

What's the value for myDate.Month and myDate.Day?

And therein lies the problem: there is no way for you to tell me this without knowing additional information.

For example, if I am running this with an English culture, the DateTime object represents October 6, 2006.  However, if I ran this same line on a computer with its culture set to French (or if the application's culture was set to French), then the DateTime object now represents a completely different date (June 10, 2006, to be precise).  This happens because French dates reverse the position of the month and the day.  

To get around this, always pass Culture information as the second argument to DateTime.Parse, as in the following:

DateTime myDate = DateTime.Parse("10/06/2006", System.Globalization.CultureInfo.GetCulture("fr-FR"));

Now, obviously you wouldn't want to do a culture retrieval every single time you call DAteTime.Parse!  Your application should probably be setting the culture itself, somewhat like the following

System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo("fr-FR");
DateTime myDate = DateTime.Parse("10/06/2006", System.Globalization.CultureInfo.CurrentCulture)

This sets the culture for the particular thread of execution that your current user is experiencing.  

You might not think that globalization errors are as embarassing as the two stories I talked about earlier.  Trust me, in the context of a globalized application, they are even worse.  Take this tip and use it to save yourself humiliation in code reviews everywhere!

Previous entries in this series:
My grandmother and the Global Assembly Cache (and Star Wars)
My grandmother and the difference between processes and threads
My grandmother and the difference between HTTP GET and HTTP POST

Monday, 26 March 2007 #