Nullable type with inline if cannot work together?

好久不见. 提交于 2019-12-17 07:49:26

问题


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?


回答1:


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.




回答2:


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))


来源:https://stackoverflow.com/questions/14633824/nullable-type-with-inline-if-cannot-work-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!