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
“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.”
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.
Use nullable datetime
Dim DOB As Nullable(Of Date) = Date.Now
Dim datestring As String = DOB.Value.ToString("d")
DOB = Nothing
“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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With