2011-05-03 67 views
63

在VB.NET中,是否有方法将DateTime变量设置为“未设置”?为什么有可能将DateTime设置为Nothing,但而不是可能检查是否为Nothing?例如:为什么我无法检查'DateTime'是否为'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. 
+1

除了John Gant的回答,您还可以检查datetime变量= Nothing(注意=而不是“is”)。 – NYSystemsAnalyst 2011-05-03 12:51:19

+0

谢谢,使用Dim boolNotSet As Boolean = d =目前没有任何东西看起来是最简单的解决方案。有趣的可空铸件在 – Muleskinner 2011-05-03 13:02:09

+0

@Chris之前从来没有见过 - 我认为他正在使用VB – 2011-05-03 13:22:13

回答

117

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

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

  • 对于值类型,这是实质上的 '零' 相当于:0IntegerFalseBooleanDateTime.MinValueDateTime,...
  • 对于引用类型,它是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 
10

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

VB有时“有用”让你觉得它做的不是。当它让你将Date设置为Nothing时,它确实将它设置为其他值,也许是MinValue。有关值类型与参考类型的广泛讨论,请参阅this question

2

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

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

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

1

在任何编程语言,使用空值时要小心。上面的例子显示了另一个问题。如果使用Nullable类型,则表示从该类型实例化的变量可以保存值System.DBNull。值;而不是它改变了使用“= 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 

然后,你可以挑选overridables让它做你的需要。很多工作 - 但如果你真的需要它,你可以做到。

3

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

(见Nullable Value Types (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)):

When checking whether a reference (or nullable value type) variable is null, do not use = Nothing or <> Nothing . Always use Is Nothing or IsNot Nothing .
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 
1

您还可以使用下面只是简单的检查:

If startDate <> Nothing Then 
your logic 
End If 

将检查日期时间数据类型的起始日期变量为空或不。

相关问题