Nullable type with inline if cannot work together?

Given the following code:

Dim widthStr As String = Nothing

This works - width is assigned Nothing:

Dim width As Nullable(Of Double)
If widthStr Is Nothing Then
  width = Nothing
Else
  width = CDbl(widthStr)
End If

But this does not - width becomes 0.0 (although it seems to be logically identical code):

Dim width As Nullable(Of Double) = If(widthStr Is Nothing, Nothing, CDbl(widthStr))

Why? Is there anything I can do to make it work?


Solution 1:

Further to Damien's answer, the clean way to do this is to not use Nothing, but New Double? instead:

Dim width As Double? = If(widthStr Is Nothing, New Double?, CDbl(widthStr))

And now that the type of the If expression is correct, this could be reduced to:

Dim width = If(widthStr Is Nothing, New Double?, CDbl(widthStr))

Solution 2:

This all comes down to type analysis of expressions.

Nothing is a magical beast in VB.Net. It's approximately the same as default(T) in C#.

As such, 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 convertible to Double (because Nothing can return the default value of value types). As such, the type of the return value of If is determined to be Double.

Only after that piece of type analysis has concluded is any attention paid to the type of the variable to which this expression is being assigned. And Double is assignable to Double? without any warnings.


There's no clean way to make your If() expression work how you expected. Because there's no equivalent to null in VB.Net. You'd need (at the least) to insert a DirectCast (or equivalent) on one side or another of the potential results of the If to force the type analysis to see Double? rather than Double.