0

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!

saminpa
  • 603
  • 1
  • 7
  • 13
  • Thank you. I was going to ask you to post it as an answer so I could accept it but it seems the protocol is to accept the "That solved my problem" – saminpa Sep 09 '18 at 21:16
  • Yup. If a duplicate solved your problem it should be accepted and the question closed. [Link-only answers](https://meta.stackexchange.com/q/8231) are discouraged. I don't mind though, glad I could help the little I did! – Visual Vincent Sep 09 '18 at 22:11

0 Answers0