It all comes down to the analysis of expression types.
Nothing is a magical beast in VB.Net. It is about the same as default(T) in C #.
Thus, when trying to determine the best type for the following:
If(widthStr Is Nothing, Nothing, CDbl(widthStr))
The third argument is of type Double . The second argument is converted to Double (because Nothing can return the default value). Thus, the type of the return value of If is defined as Double .
Only after this part of the type analysis is completed does any attention be paid to the type of variable to which this expression is assigned. And is Double assigned to Double? without any warning.
There is no pure way to make your If() expression work as you expected. Because there is no null equivalent in VB.Net. Do you need to (at least) insert a DirectCast (or equivalent) with one or the other of the potential If results to make the type analysis see Double? , not Double .
source share