Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 为什么可以';我是否检查a';日期时间';是';什么都没有;?_Vb.net_Datetime_Null_Nullable_Nothing - Fatal编程技术网

Vb.net 为什么可以';我是否检查a';日期时间';是';什么都没有;?

Vb.net 为什么可以';我是否检查a';日期时间';是';什么都没有;?,vb.net,datetime,null,nullable,nothing,Vb.net,Datetime,Null,Nullable,Nothing,在VB.NET中,是否有方法将DateTime变量设置为“未设置”?为什么可以将日期时间设置为无,但无法检查它是否为无?例如: Dim d As DateTime = Nothing Dim boolNotSet As Boolean = d Is Nothing 第二条语句引发此错误: “Is”运算符不接受“Date”类型的操作数。操作数必须是引用或 可为空的类型。 DateTime是一种值类型,因此不能为null。您可以检查它是否等于DateTime.MinValue,也可以使用null

在VB.NET中,是否有方法将
DateTime
变量设置为“未设置”?为什么可以将
日期时间
设置为
,但无法检查它是否为
?例如:

Dim d As DateTime = Nothing
Dim boolNotSet As Boolean = d Is Nothing 
第二条语句引发此错误:

“Is”运算符不接受“Date”类型的操作数。操作数必须是引用或
可为空的类型。

DateTime是一种值类型,因此不能为null。您可以检查它是否等于
DateTime.MinValue
,也可以使用
null(日期时间的)

VB有时会“有益地”让你认为它在做它不做的事情。当它允许您将日期设置为Nothing时,实际上是将其设置为其他值,可能是MinValue


有关值类型与引用类型的详细讨论,请参阅。

DateTime是一种值类型,这意味着它总是有一些值

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


如果您想要一个不带任何值的DateTime,请使用可为空的DateTime。

这是与VB.Net、IMO混淆的最大原因之一

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

  • 对于值类型,这本质上等同于“零”:
    0
    表示
    Integer
    False
    表示
    Boolean
    DateTime.MinValue
    表示
    DateTime
  • 对于引用类型,它是
    null
    值(一个不引用任何内容的引用)
因此,语句
d Is Nothing
相当于
d Is DateTime.MinValue
,这显然不可编译

解决办法:正如其他人所说

  • 使用
    DateTime?
    (即
    null(日期时间)
    )。这是我首选的解决方案
  • 或者使用
    d=DateTime.MinValue
    或等效的
    d=Nothing
在原始代码的上下文中,您可以使用:

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

在任何编程语言中,都可以在

上找到更全面的解释,使用null时要小心。上面的例子显示了另一个问题。如果您使用的是Nullable类型,这意味着从该类型实例化的变量可以保存值System.DBNull.value;并不是因为它改变了使用“=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
末级

'在Main():


然后,您可以选择覆盖项,以使其满足您的需要。大量工作—但如果您确实需要它,您可以这样做。

有关使用nullable
DateTime
值的一些示例

(有关更多信息,请参阅。)

'
'一个普通的日期时间声明。它是*不*可为空的。设置为
“Nothing”实际上导致非空值。
'
Dim d1作为日期时间=无
Console.WriteLine(String.Format(“d1=[{0}]\n”,d1))
'输出:d1=[1/1/0001 12:00:00 AM]
'Console.WriteLine(String.Format(“d1什么都不是?[{0}]\n”,(d1什么都不是)))
'
“上述表达式上的编译错误”(d1为Nothing):
'
“”Is“”运算符不接受类型为“Date”的操作数。
'操作数必须是引用类型或可为空的类型。
'
'声明日期时间的三种不同但等效的方法
'可为空:
'
昏暗的d2?日期时间=无
Console.WriteLine(String.Format(“d2=[{0}][{1}]\n”,d2,(d2什么都不是)))
'输出:d2=[]正确]
将d3变暗为日期时间?=没有什么
Console.WriteLine(String.Format(“d3=[{0}][{1}]\n”,d3,(d3什么都不是)))
'输出:d3=[]正确]
Dim d4可为空(日期时间)=无
Console.WriteLine(String.Format(“d4=[{0}][{1}]\n”,d4,(d4什么都不是)))
'输出:d4=[]正确]
另外,关于如何检查变量是否为null(从):

检查引用(或可空值类型)变量是否为null,请不要使用
=Nothing
Nothing
。始终使用
是空的
不是空的

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

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

您也可以使用以下简单的方法进行检查:

If startDate <> Nothing Then
your logic
End If
如果起始日期为空,则
你的逻辑
如果结束

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

您可以通过如下方式进行检查:

if varDate = "#01/01/0001#" then
       '  blank date. do something.
else
       ' Date is not blank. Do some other thing
end if

除了下面John Gant的答案之外,您还可以检查datetime变量是否为Nothing(请注意,=而不是“is”)。谢谢,使用Dim boolNotSet作为Boolean=d=Nothing似乎是目前最简单的解决方案。有趣的是,可调为零的施法从未见过这种情况before@Chris-我想他正在使用VB@KarthikRatnam,是的,他是,但这与@Marc Gravell的答案是一样的。@NYSystems分析师:根据,使用
=Nothing
Nothing
是不好的做法:检查引用(或可空值类型)变量是否为空时,不要使用
=Nothing
Nothing
。始终使用
Is Nothing
isNothing
“感谢您的解释。有趣的是,isNothing(d)不起作用,但d=Nothing不起作用!
if varDate = "#01/01/0001#" then
       '  blank date. do something.
else
       ' Date is not blank. Do some other thing
end if