I was trying to compact my code by combining a declaration and a validation for a nullable datetime (because I want to store timestamps in a specific format and DateTime.MinValue essentially causes an overflow under certain circumstances).
The code below shows two ways to do the validation:
Dim provider As Globalization.CultureInfo = System.Globalization.CultureInfo.InvariantCulture
Dim style As Globalization.DateTimeStyles = Globalization.DateTimeStyles.None
Dim xe As New XElement("DT")
Debug.Print(IsNothing(xe))
Debug.Print(xe.IsEmpty)
Dim dt As DateTime? = If(xe.IsEmpty, Nothing, Date.ParseExact(xe.Value, "yyyyMMddHHmmss", Provider, style))
Debug.Print(dt)
dt = Nothing
Debug.Print(IsNothing(dt))
Dim dt2 As DateTime?
If Not xe.IsEmpty Then dt2 = Date.ParseExact(xe.Value, "yyyyMMddHHmmss", provider, style)
Debug.Print(IsNothing(dt2))
The output confirms that xe IsEmpty and xe is not Nothing but dt = DateTime.MinValue instead of Nothing and dt2 (correctly) is Nothing.
Can someone please point out the glaringly obvious thing I am missing, why is dt <> dt2??
Thanks!