83

在 VB.NET 中,有没有办法将DateTime变量设置为“未设置”?为什么可以将 a 设置DateTimeNothing,但无法检查它是否是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.
4

8 回答 8

146

这是与 VB.Net、IMO 混淆的最大来源之一。

Nothing在 VB.Net 中相当于default(T)在 C# 中:给定类型的默认值。

  • 对于值类型,这本质上相当于“零”:0for Integer, Falsefor Boolean, DateTime.MinValuefor DateTime, ...
  • 对于引用类型,它是null值(一个引用,嗯,什么都没有)。

因此,该语句d Is Nothing等效于d Is DateTime.MinValue,显然无法编译。

解决方案:正如其他人所说

  • 要么使用DateTime?(即Nullable(Of DateTime))。这是我的首选解决方案。
  • 或使用d = DateTime.MinValue或等效d = Nothing

在原始代码的上下文中,您可以使用:

Dim d As DateTime? = Nothing
Dim boolNotSet As Boolean = d.HasValue

更全面的解释可以在Anthony D. Green 的博客上找到

于 2011-05-03T13:06:11.373 回答
14

DateTime 是一个值类型,这就是它不能为空的原因。您可以检查它是否等于DateTime.MinValue,或者您可以使用它Nullable(Of DateTime)来代替。

VB 有时会“有帮助”让您认为它正在做一些实际上并非如此的事情。当它允许您将 Date 设置为 Nothing 时,它实际上是在将其设置为其他值,可能是 MinValue。

有关值类型与引用类型的广泛讨论,请参阅此问题

于 2011-05-03T12:49:31.633 回答
5

DateTime 是一个值类型,这意味着它总是有一些价值。

它就像一个整数——它可以是 0,或 1,或小于零,但它永远不可能是“无”。

如果您想要一个可以取值为 Nothing 的 DateTime,请使用 Nullable DateTime。

于 2011-05-03T12:50:22.500 回答
4

有关使用可为空DateTime值的一些示例。

(有关更多信息,请参阅可空值类型 (Visual Basic)。)

'
' An ordinary DateTime declaration. It is *not* nullable. Setting it to
' 'Nothing' actually results in a non-null value.
'
Dim d1 As DateTime = Nothing
Console.WriteLine(String.Format("d1 = [{0}]\n", d1))
' Output:  d1 = [1/1/0001 12:00:00 AM]

' Console.WriteLine(String.Format("d1 is Nothing? [{0}]\n", (d1 Is Nothing)))
'
'   Compilation error on above expression '(d1 Is Nothing)':
'
'      'Is' operator does not accept operands of type 'Date'.
'       Operands must be reference or nullable types.

'
' Three different but equivalent ways to declare a DateTime
' nullable:
'
Dim d2? As DateTime = Nothing
Console.WriteLine(String.Format("d2 = [{0}][{1}]\n", d2, (d2 Is Nothing)))
' Output:  d2 = [][True]

Dim d3 As DateTime? = Nothing
Console.WriteLine(String.Format("d3 = [{0}][{1}]\n", d3, (d3 Is Nothing)))
' Output:  d3 = [][True]

Dim d4 As Nullable(Of DateTime) = Nothing
Console.WriteLine(String.Format("d4 = [{0}][{1}]\n", d4, (d4 Is Nothing)))
' Output:  d4 = [][True]

另外,关于如何检查变量是否为(来自Nothing (Visual Basic)):

检查引用(或可为空值类型)变量是否为null时,不要使用= Nothingor <> Nothing。始终使用Is NothingIsNot Nothing
于 2012-09-12T14:44:24.153 回答
1

在任何编程语言中,使用 Null 时都要小心。上面的示例显示了另一个问题。如果使用 Nullable 类型,则意味着从该类型实例化的变量可以保存值 System.DBNull.Value;并不是说它改变了使用“= Nothing”将值设置为默认值的解释,或者值的对象现在可以支持空引用。只是一个警告......快乐的编码!

您可以创建一个包含值类型的单独类。从这样的类创建的对象将是一个引用类型,它可以被赋值为 Nothing。一个例子:

Public Class DateTimeNullable
Private _value As DateTime

'properties
Public Property Value() As DateTime
    Get
        Return _value
    End Get
    Set(ByVal value As DateTime)
        _value = value
    End Set
End Property

'constructors
Public Sub New()
    Value = DateTime.MinValue
End Sub

Public Sub New(ByVal dt As DateTime)
    Value = dt
End Sub

'overridables
Public Overrides Function ToString() As String
    Return Value.ToString()
End Function

结束类

'在主():

        Dim dtn As DateTimeNullable = Nothing
    Dim strTest1 As String = "Falied"
    Dim strTest2 As String = "Failed"
    If dtn Is Nothing Then strTest1 = "Succeeded"

    dtn = New DateTimeNullable(DateTime.Now)
    If dtn Is Nothing Then strTest2 = "Succeeded"

    Console.WriteLine("test1: " & strTest1)
    Console.WriteLine("test2: " & strTest2)
    Console.WriteLine(".ToString() = " & dtn.ToString())
    Console.WriteLine(".Value.ToString() = " & dtn.Value.ToString())

    Console.ReadKey()

    ' Output:
    'test1:  Succeeded()
    'test2:  Failed()
    '.ToString() = 4/10/2012 11:28:10 AM
    '.Value.ToString() = 4/10/2012 11:28:10 AM

然后,您可以挑选并选择可覆盖的内容以使其满足您的需求。很多工作 - 但如果你真的需要它,你可以做到。

于 2012-04-10T15:11:55.930 回答
1

您也可以使用下面的简单检查:

If startDate <> Nothing Then
your logic
End If

它将检查 DateTime 数据类型的 startDate 变量是否为空。

于 2014-12-30T12:18:13.227 回答
1

您可以像下面这样检查:

if varDate = "#01/01/0001#" then
       '  blank date. do something.
else
       ' Date is not blank. Do some other thing
end if
于 2018-05-19T21:37:43.810 回答
0

解决此问题的一种方法是改用 Object 数据类型:

Private _myDate As Object
Private Property MyDate As Date
    Get
        If IsNothing(_myDate) Then Return Nothing
        Return CDate(_myDate)
    End Get
    Set(value As Date)
        If date = Nothing Then
            _myDate = Nothing
            Return
        End If
        _myDate = value
     End Set
End Property

然后您可以将日期设置为如下所示:

MyDate = Nothing
Dim theDate As Date = MyDate
If theDate = Nothing Then
    'date is nothing
End If
于 2013-07-14T12:55:07.193 回答