【发布时间】:2019-02-21 04:10:27
【问题描述】:
在 VB.NET 中,有没有办法将 DateTime 变量设置为“未设置”?为什么可以将DateTime 设置为Nothing,但不能 可以检查它是否为Nothing?例如:
Dim d As DateTime = Nothing
Dim boolNotSet As Boolean = d Is Nothing
第二条语句抛出这个错误:
'Is' operator does not accept operands of type 'Date'. Operands must be reference or
nullable types.
【问题讨论】:
-
除了下面 John Gant 的回答,您还可以检查 datetime 变量是否 = Nothing(注意 = 而不是“is”)。
-
谢谢,使用 Dim boolNotSet As Boolean = d = 目前似乎没有最简单的解决方案。以前从未见过的 Nullable 强制转换很有趣
-
@Chris - 我认为他正在使用 VB
-
@Karthik Ratnam,是的,他是,但它等同于同一件事,@Marc Gravell 的回答得到了所有要点。
-
@NYSystemsAnalyst:根据Nothing (Visual Basic),使用
= Nothing或<> Nothing不是好的做法:“检查引用(或可空值类型)变量是否为null,不要使用= Nothing或<> Nothing。始终使用Is Nothing或IsNot Nothing。"
标签: vb.net datetime null nullable nothing