我發現在VB.NET一個簡單的錯誤,可以很容易地複製:VB.NET如果(三元)錯誤
Dim pDate As Date?
Dim pString As String = ""
' works fine as expected
pDate = If(False, "", Nothing)
' expected: pDate will be set to Nothing.
' BUG: Conversion from string "" to type 'Date' is not valid.
pDate = If(False, pString, Nothing)
'These both fail with the same error
pDate = pString
Dim pDate2 As Date? = ""
問:這是一個錯誤?或者我或我的電腦有問題嗎? 如果這是一個錯誤,是否有這個錯誤報告(我似乎無法找到它)?
教訓:
- 這是不是一個錯誤
- 可空日期接受對象沒有
- 可空日期接受字符串沒有
pDate = Nothing ' ok. nullable date accepts object nothing
pString = Nothing
pDate = pString ' error. nullable date rejects string nothing
這是一個錯誤?你*不能*將'''''轉換爲'日期'。即使'If'(與'IIf'相反)在運行時只計算一邊並不意味着編譯器不檢測類型不匹配。 –
是的,我知道編譯器仍然檢測到類型不匹配,但是(pDate =「」)是有效的,而(pDate = pString)不是(pString =「」) – Arvin
pDate =「」如果Option嚴格或選項推斷開啓。 –