【发布时间】:2011-05-10 13:06:15
【问题描述】:
我在 VB.NET (VS 2010) 中遇到 Nullable DateTime 问题。
方法一
If String.IsNullOrEmpty(LastCalibrationDateTextBox.Text) Then
gauge.LastCalibrationDate = Nothing
Else
gauge.LastCalibrationDate = DateTime.Parse(LastCalibrationDateTextBox.Text)
End If
方法二
gauge.LastCalibrationDate = If(String.IsNullOrEmpty(LastCalibrationDateTextBox.Text), Nothing, DateTime.Parse(LastCalibrationDateTextBox.Text))
当给定一个空字符串时,方法 1 将 Null(无)值分配给 gauge.LastCalibrationDate,但方法 2 为其分配 DateTime.MinValue。
在我的代码的其他地方我有:
LastCalibrationDate = If(IsDBNull(dr("LastCalibrationDate")), Nothing, dr("LastCalibrationDate"))
这将三元运算符中的 Null(无)正确分配给 Nullable DateTime。
我错过了什么?谢谢!
【问题讨论】:
-
请您添加您在代码中使用的 gauge.LastCalibrationData 定义吗?
标签: vb.net datetime nullable ternary-operator