Thursday, October 20, 2005

To cast or not to cast

There are times when programming is a breeze and there are times when you just want to scream. I'm working on some code to replicate the SQL Server schedule task dialog in a C# web application.

As part of the code I had the following (txtRepeatTimeStart is a TextBox control):

DateTime.Now.Date + " " + txtRepeatTimeStart

This is being passed into a constructor that has a string parameter. Now I'd expect this to cause a build error as there are three different things here - a DateTime, a String and a TextBox. But no! It builds and, as far as I can see, is doing the ToString() on the DateTime and TextBox automatically.

A wasted 20 minutes until I realised that I was stupidly trying to concatenate the wrong things together when you'd expect it to be a build error.

No comments: