Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • Last Modified:

Weird IF behavior in C#

I have this IF/ELSE in my code and it works perfectly:

     if (ID == 3)
          SelectedRow ["LocationID"] = System.DBNull.Value;
     else
          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)?

Thanks,
Brian
0
bek
Asked:
bek
  • 3
  • 2
  • 2
  • +2
1 Solution
 
YurichCommented:
i think you have an example of implicit casting.

in your first if-else block  SelectedRow[""] is casted to DBNull type as well as to int type and i assume you'll have to do checking when you try getting values back from your SelectedRow[""] since casting from System.DBNull to anything else is not possible.

Thus, in your second if statement, you have a type collision since compiler doesn't know at the compile time to what type SelectedRow should be casted to and throws exception...

I don't think you can make it working having both DBNull and int types in the same statement.

regards
0
 
nil1stCommented:
Brian,
first of two if statments you described above are not equal
in first If statment you have "if(ID == 3)"
and 2nd (immediate if statment) you have "if(ID != 3)

thus I am assuming that when you run the first if statment, it does not reaches the statment System.DBNull.Value instead it just goes to SelectedRow[Location ID] = ID statment.

However when you run the Immediate if, it goes to the System.DBNull.Value, as a result 2nd one bombs out.

maybe try putting
SelectedRow[Location ID] = System.DBNull.Value;
without any if statment just to test my theory, and see if it bombs out...

Thanks

Nirav
0
 
YurichCommented:
no, it won't and doesn't need to reach the statement to do type-checking - it's all done during the compile time. If it was an error, it would give an error while compiling.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
cyberdevil67Commented:
Hi bek,

 Yep it must be the same typecast, Object.Null is not an Int and Int is not an Object Type cast. Maybe 0 instead would be better.

Cheers!
0
 
b1xml2Commented:
SelectedRow ["LocationID"] = (ID != 3) ? (object)System.DBNull.Value : (object)ID;
0
 
bekAuthor Commented:
Sorry for the confusion, guys.  The immediate IF statement had a small typo.  Both IF statements actually said (ID == 3).  Both lines do compile, because the error is at compile-time.

b1xml2, your line of code works fine.  SelectedRow ["LocationID"] accepts an int, like this:

SelectedRow ["LocationID"] = 3;

But it also accepts an object, like in your example.  Any idea why it will work with both types?
0
 
b1xml2Commented:
the conditional operator ?: expects the data type when the expression is true or false to be of the same type.
0
 
b1xml2Commented:
if you see carefully,
SelectedRow["LocationID"] is of the type object. That is true for all untyped datatables. and since both DBNull.Value as well as an integer are objects, they can be boxed as objects.
0
 
bekAuthor Commented:
Interesting!

I was thinking '3' is a primitive.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now