Nullable type with inline if cannot work together?

前端 未结 2 528
鱼传尺愫
鱼传尺愫 2020-11-30 13:55

Given the following code:

Dim widthStr As String = Nothing

This works - width is assigned Nothing:



        
2条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 14:37

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

提交回复
热议问题