Thursday, 16 July 2009

VS2010 Training Kit - US-centric hardcoded dates

Finally got my virtual machine with Windows 7 and VS2010 beta sorted so I started working through the Training Kit, wanting to mess with the Parallel stuff. Ran the first code snippet and BANG! I'm getting a parse exception. Turns out the startdates for the Employees are being created using (e.g.):

DateTime.Parse("5/16/2001")

Which is fine in the States but no use on my en-GB machine. So my first instinct was to run a regular expression to turn them all into (e.g.):

new DateTime(2001, 5, 16)

But then I realised that my RegEx skills are not good enough and I wanted to get the job done before my train home finally arrived. After umming and ahhing for a few moments I remembered that the problem was my culture settings. So I thought I have two options - either change my culture settings (BOO!) or change the constructor's culture settings.

So I stuck this at the start of the EmployeeList constructor:

var ct = Thread.CurrentThread;
var origCulture = ct.CurrentCulture;
ct.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");

And this at the end:


ct.CurrentCulture = origCulture;


Yay!

No comments: