Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Date variable to null in VB.NET

Tags:

date

null

vb.net

I want to assign the DOB variable as null.

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
like image 364
gaurav somani Avatar asked Dec 06 '12 08:12

gaurav somani


People also ask

How to assign null value to date variable in vb net?

“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.”

How do you give a date a null value?

The Nullable < T > structure is using a value type as a nullable type. By default DateTime is not nullable because it is a Value Type, using the nullable operator introduced in C# 2, you can achieve this. Using a question mark (?) after the type or using the generic style Nullable.


2 Answers

Use nullable datetime

    Dim DOB As Nullable(Of Date) = Date.Now
    Dim datestring As String = DOB.Value.ToString("d")
    DOB = Nothing
like image 101
indiPy Avatar answered Oct 22 '22 15:10

indiPy


“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.”

VB.NET - Nullable DateTime and Ternary Operator

like image 42
iOSAndroidWindowsMobileAppsDev Avatar answered Oct 22 '22 16:10

iOSAndroidWindowsMobileAppsDev