Tuesday, May 27, 2008

It just keeps getting easier...

I have just started playing around with Visual Studio 2008, but I can see that life as I know it is going to change...it is going to be getting easier.

For all of you non-programmers in the audience, it is not so much HOW you get to a solution, it's who can get there in the fewest lines of code. This new feature of VS2008 should help...

In earlier versions of .NET if we wanted to 'protect' some code against cast exceptions we used a 'TRY..CATCH...FINALLY' block to do it. So that meant that we read in the information from say a textbox, tried to do a conversion to an Integer (or some other number data type) and then crossed our fingers and hoped that a exception didn't occur because we could not go back and let a user change their entry.

We have used validation (see the 10 commandments of programming...just kidding, I will post those later), but where is the fun in that...remember the fewer lines of code, the better.

Enter the Int32.TryParse command (you can change the Int32 to another data type if you prefer). You can grab the data, do a type check and if there is an error take care of the error in an IF...THEN block all with a couple lines of code. Take that Framework 1.1!

No comments: