Why I can't set Nothing to Nullable(Of Double) through conditional ternary operator but I can directly?
Dim d As Double? = Nothing
d = If(True, 0, Nothing) ' result: d = 0
d = Nothing ' result: d = Nothing
d = If(False, 0, Nothing) ' result: d = 0 Why?
Edit: These work (based on below accepted answer):
d = If(False, 0, New Integer?)
d = If(False, CType(0, Double?), Nothing)
d = If(False, 0, CType(Nothing, Double?))
Nothing
converts to a lot of types, not justT?
. It can happily convert toDouble
:or to
Integer
. It's that sense ofNothing
that you're using inIf(X, 0, Nothing)
, becauseIf
needs the second and third arguments to match in type: it treats it as typeInteger
, because that's the type of0
.Explicitly specifying one of the types as nullable (either
Integer?
orDouble?
would work) lets the compiler figure out what you want:d = If(False, CType(0, Double?), Nothing)
, ord = If(False, 0, CType(Nothing, Double?))