Wednesday, July 21, 2010

People who follow me on Twitter might have seen my tweets about going on a weekend to Holland with some friends. Next to excessive eating and drinking, we took the time to do some sightseeing in The Hague.

The sight which gave us the most interesting pictures is Madurodam. Madurodam is a miniature city featuring the most famous Dutch buildings and landmarks. Hope you enjoy these pictures!

Wednesday, July 14, 2010

Last week I came across a scenario where I wanted to switch over non-constants (aka variables), but while I was compiling I got Compiler Error CS0150 (A constant value is expected). This is one of those things I always forget. You can't use variables in your case statements because the C# compiler doesn't allow you to. It's very logical though, the compiler forces you to use constants because otherwise there is no way of knowing there are equal case statements.

The scenario

Let's say I have an ASP.NET page where the user can input and submit a value. On the server-side I want to match this value with a value from a local resource file. Depending on the match I want to execute other code.

Remember I can't use a switch because the values in the local resource file are variable.

Option one: Using else-if statements

As shown in the code snippet below you can use else-if statements to search for a match.

1: if (input == GetLocalResourceObject("CaseOne").ToString())

2: {

3: this.ltResult.Text = "Case one matched.";

4: }

5: elseif (input == GetLocalResourceObject("CaseTwo").ToString())

6: {

7: this.ltResult.Text = "Case two matched.";

8: }

9: elseif (input == GetLocalResourceObject("CaseThree").ToString())

10: {

11: this.ltResult.Text = "Case three matched.";

12: }

13: else

14: {

15: this.ltResult.Text = "No matching case found.";

16: }

This option has at least two disadvantages:

It allows equal conditions (cases) which might have horrible consequences.

It's ugly.

Option two: Using a dictionary

You can also use a generic dictionary where the keyvaluepairs have a string as the key and a delegate as the value. Because the code I'm executing when a match is found is so compact I'm using the simplest delegate of them all: an Action delegate. An Action delegate takes no parameters and does not return a value.

Thanks for pointing out the things that were still wrong. These improvements have taken this code snippet to another level again. This also proves how having an extra pair of eyes can improve the quality of your code significantly!

Monday, July 12, 2010

Last week I came across a problem which seemed trivial at first sight, but turned out to be a pretty fun refactoring challenge.

The scenario

The real problem was a bit more complex, but for simplicity's sake I made this example. In this form there are three checkboxes which map with the textboxes next to them. If the checkbox gets unchecked the corresponding textboxes should be cleared.

This is better, but I'm still not satisfied. Those three eventhandlers seem to be overhead, and are just cluttering my code-behind.

Third and final iteration

In the final iteration I get rid of the three seperate eventhandlers. I use a dictionary to create a mapping between the checkboxes and their corresponding textboxes. Using the eventhandlers sender I get the matching mapping in the dictionary. Once the matching mapping is found I extract the checkbox and its corresponding textboxes from the keyvaluepair and apply the logic.