Weird IF behavior in C#
Posted on 2005-05-01
I have this IF/ELSE in my code and it works perfectly:
if (ID == 3)
SelectedRow ["LocationID"] = System.DBNull.Value;
SelectedRow ["LocationID"] = ID;
The equivalent Immediate IF statement bombs! The problem seems to be that I'm trying to assign either an int or a null. Something about C# doesn't like the fact that SelectedRow ["LocationID"] can be either value (but it works fine in the IF above, just not the Immediate IF). Here's the Immediate IF statement that bombs:
SelectedRow ["LocationID"] = (ID != 3) ? System.DBNull.Value : ID;
And here's the error message:
Type of conditional expression can't be determined because there is no implicit conversion between 'System.DBNull' and 'int'
It's not a huge deal, because the first IF works fine, but I am really curious... does anyone know why the Immediate IF doesn't work, and/or how to correct the behavior (if possible)?