I am deeply frustrated at this point in time. The reason is because I have been spending the last 30 minutes hung over a very careless mistake. C# has the coalesce operator that allows you to write code that looks neater when you are doing complex stuff like LINQ. The problem looked something like this - I had to read a couple of rows from the database and convert those data as properties on an object.
the code would look something like this:
The coalesce operator allows you to make the code more concise by saying
but whilst reading data from a database table, if a particular cell is a null value, it is not read as a null but it is read as a System.DBNull. So, the freaking row[field] read was getting read as a DBNull. Once I noticed that this was the problem, I coul;d fix the code, but the fix was less elegant.
More edits on this post when I find more about it.
[Edit 1]
the code would look something like this:
object x; int value; if (x == null) value = -1; // default value else value = (int)x;
The coalesce operator allows you to make the code more concise by saying
int value = x ?? -1;// less code = less maintainence
but whilst reading data from a database table, if a particular cell is a null value, it is not read as a null but it is read as a System.DBNull. So, the freaking row[field] read was getting read as a DBNull. Once I noticed that this was the problem, I coul;d fix the code, but the fix was less elegant.
More edits on this post when I find more about it.
[Edit 1]
Found this related question on Stackoverflow [here]