如何在 VB.NET 中将 Date 变量设置为 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13739708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set Date variable to null in VB.NET
提问by gaurav somani
I want to assign the DOB
variable as null.
我想将DOB
变量分配为空。
Dim DOB As Date = DTPSdob.Value
Dim datestring As String = DOB.ToString("d")
If chkSdob.Checked = False Then
DOB = 'I want to assign here DOB variable as null'
Else
DOB = datestring
End If
回答by py2020
Use nullable datetime
使用可为空的日期时间
Dim DOB As Nullable(Of Date) = Date.Now
Dim datestring As String = DOB.Value.ToString("d")
DOB = Nothing
回答by iOSAndroidWindowsMobileAppsDev
“DateTime is a value type (a structure) and can not be set to null or nothing as with all .Net value types. The default value for a datetime value is zeros for the date portion and 12:00:00 AM for the Time portion.”
“DateTime 是一种值类型(一种结构),不能像所有 .Net 值类型一样设置为 null 或不设置。日期时间值的默认值对于日期部分是零,对于时间部分是 12:00:00 AM。”
回答by Harry B.
Change the DOB from a date variable to an object variable. You can then pass nothing or a valid date into the variable. That variable can then be used update the database.
将 DOB 从日期变量更改为对象变量。然后,您可以不传递任何内容或有效日期到变量中。然后可以使用该变量更新数据库。
dim DOB as object = nothing
If chkSdob.Checked = True Then
DOB = datestring
end if