Locked passing Null dates as parameters to functions

  • Wednesday, May 03, 2006 7:23 AM
     
     

    Hi

    What's the best way of passing in Null / empty date fields as parameters to functions ?


    What's the best way of passing in a date into a VB function as a null/empty value - at the moment I am checking to see if it = #12:00:00 AM# , but this seems somewhat haphazard !

    thanks
    Bruce

     

All Replies

  • Wednesday, May 03, 2006 7:30 AM
     
     Answered

    Test if date variable is nothing:

        Dim testDate As Date = Nothing

     

        MsgBox(DateIsNull(testDate))

     

        testDate = Date.Now

     

        MsgBox(DateIsNull(testDate))

     

     

        Private Function DateIsNull(ByVal testDate As Date) As Boolean

            If testDate.Equals(Nothing) Then

                Return True

            End If

     

            Return False

        End Function

     

  • Wednesday, May 03, 2006 2:28 PM
    Moderator
     
     

    If you are using VB 2005, you can use the Nullable(Of Date) as your parameter. Of course, that would mean you would need to either use a Nullable(Of Date) property/field that you were passing or create a new Nullable(Of Date) that you pass in to the method. Once you have a Nullable(Of Date), you can see if it is empty by checking the .HasValue property. Here's a small snippet:

    Dim x As New Nullable(Of Date)

    x = #1/1/2006#

    Debug.WriteLine(x.HasValue)

    Jim Wooley
    http://devauthority.com/blogs/jwooley/default.aspx