我在VB.NET(VS 2010)中遇到了Nullable DateTime的问题。
方法1
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(nothing)赋值给gauge.LastCalibrationDate,但方法2会将其赋值为DateTime.MinValue。在我的代码的其他地方,我有:
LastCalibrationDate = If(IsDBNull(dr("LastCalibrationDate")), Nothing, dr("LastCalibrationDate"))
这个三元运算符将 Null (Nothing) 赋值给 Nullable DateTime 变量,写法正确。
我还有什么遗漏的吗?谢谢!