when srcCell.CELL_VALUE_FLOAT doesn’t mysteriously evaluate the True part!
No, it is not. It simply evaluates the false part ( Nothing ) as 0 , while setting CELL_VALUE_INT to 0.
Let me clarify: expression
Dim i As Integer? = If(False, 1, Nothing)
fills i with 0 . (Check it out if you don't believe me.)
Why is this happening? Nothing in VB.NET is not null in C #. If used with a value type, Nothing means "the default value for this type." If indicates Integer (not Integer? ) As the common type for 1 and Nothing , and thus evaluates Nothing as default(Integer) = 0 .
You can fix it as follows:
Dim i As Integer? = If(False, 1, DirectCast(Nothing, Integer?))
which in your example means
dstCell.CELL_VALUE_INT = If(srcCell.CELL_VALUE_FLOAT IsNot Nothing, Math.Round(CDbl(srcCell.CELL_VALUE_FLOAT)), DirectCast(Nothing, Integer?))
This should now give the correct meaning.
Since this is a pretty amazing behavior, I added some Microsoft Connect suggestion to add a compiler warning.
source share