Returning Nothing from Function

Discussion in 'ASP .Net' started by James, Apr 19, 2008.

  1. James

    James Guest

    Hello,

    I am trying to create a Function to test TextBoxes and convert the Text to
    DateTime if not empty.

    My code is :

    Public Shared Function GetDateTime(ByVal DateValue As String) As String

    If Not String.IsNullOrEmpty(DateValue) Then
    Return DateTime.Parse(DateValue)
    Else
    Return Nothing
    End If

    End Function

    Then in a OnClick command I have:

    CurrentMember.StartDate = GetDateTime(txtStartDate.Text)

    It works ok when there is a date but if its empty I get an error message:
    System.InvalidCastException: Conversion from string "" to type 'Date' is not
    valid.

    If I do CurrentMember.StartDate = Nothing it works fine so no problem with
    Null values in the field.

    Does anyone have any suggestions as this has puzzled me for too long now.

    Thanks in advance
    James
    James, Apr 19, 2008
    #1
    1. Advertising

  2. James

    sloan Guest

    You can try this.


    Public Shared Function GetDateTime(ByVal DateValue As String) As String

    dim returnValue as DateTime = DateTime.MinValue

    If Not String.IsNullOrEmpty(DateValue) Then
    DateTime.TryParse(DateValue, out returnValue ) ''Check Syntax
    here, but it should be right..I'm primarily c# now


    End If


    if ( returnValue = DateTime.MinValue ) then
    return Nothing
    end if

    return returnValue


    End Function


    Or research "nullable".





    "James" <> wrote in message
    news:...
    > Hello,
    >
    > I am trying to create a Function to test TextBoxes and convert the Text to
    > DateTime if not empty.
    >
    > My code is :
    >
    > Public Shared Function GetDateTime(ByVal DateValue As String) As String
    >
    > If Not String.IsNullOrEmpty(DateValue) Then
    > Return DateTime.Parse(DateValue)
    > Else
    > Return Nothing
    > End If
    >
    > End Function
    >
    > Then in a OnClick command I have:
    >
    > CurrentMember.StartDate = GetDateTime(txtStartDate.Text)
    >
    > It works ok when there is a date but if its empty I get an error message:
    > System.InvalidCastException: Conversion from string "" to type 'Date' is
    > not
    > valid.
    >
    > If I do CurrentMember.StartDate = Nothing it works fine so no problem with
    > Null values in the field.
    >
    > Does anyone have any suggestions as this has puzzled me for too long now.
    >
    > Thanks in advance
    > James
    sloan, Apr 19, 2008
    #2
    1. Advertising

  3. "James" <> wrote in message
    news:...
    > Hello,
    >
    > I am trying to create a Function to test TextBoxes and convert the Text to
    > DateTime if not empty.
    >
    > My code is :
    >
    > Public Shared Function GetDateTime(ByVal DateValue As String) As String
    >
    > If Not String.IsNullOrEmpty(DateValue) Then
    > Return DateTime.Parse(DateValue)
    > Else
    > Return Nothing
    > End If
    >
    > End Function
    >
    > Then in a OnClick command I have:
    >
    > CurrentMember.StartDate = GetDateTime(txtStartDate.Text)
    >
    > It works ok when there is a date but if its empty I get an error message:
    > System.InvalidCastException: Conversion from string "" to type 'Date' is
    > not
    > valid.
    >
    > If I do CurrentMember.StartDate = Nothing it works fine so no problem with
    > Null values in the field.
    >
    > Does anyone have any suggestions as this has puzzled me for too long now.
    >
    > Thanks in advance
    > James



    If Not String.IsNullOrEmpty(DateValue) AND DateValue <> String.Empty Then
    ThatsIT.net.au, Apr 19, 2008
    #3
  4. "James" <> wrote in message
    news:...
    > Hello,
    >
    > I am trying to create a Function to test TextBoxes and convert the Text to
    > DateTime if not empty.
    >
    > My code is :
    >
    > Public Shared Function GetDateTime(ByVal DateValue As String) As String
    >
    > If Not String.IsNullOrEmpty(DateValue) Then
    > Return DateTime.Parse(DateValue)
    > Else
    > Return Nothing
    > End If
    >
    > End Function
    >
    > Then in a OnClick command I have:
    >
    > CurrentMember.StartDate = GetDateTime(txtStartDate.Text)
    >
    > It works ok when there is a date but if its empty I get an error message:
    > System.InvalidCastException: Conversion from string "" to type 'Date' is
    > not
    > valid.
    >
    > If I do CurrentMember.StartDate = Nothing it works fine so no problem with
    > Null values in the field.
    >
    > Does anyone have any suggestions as this has puzzled me for too long now.
    >
    > Thanks in advance
    > James


    sorry misread your post

    Function GetDateTime(ByVal DateValue As String) As String
    Try
    Return DateTime.Parse(DateValue)
    Catch ex As Exception
    Return Nothing
    End Try
    End Function
    ThatsIT.net.au, Apr 19, 2008
    #4
  5. James

    sloan Guest

    You should not use Exception catching as a part of normal business flow.


    http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.aspx


    I would stick with my TryParse version before I'd use the catch exception
    version.


    ...





    "ThatsIT.net.au" <me@work> wrote in message
    news:...
    >
    > "James" <> wrote in message
    > news:...
    >> Hello,
    >>
    >> I am trying to create a Function to test TextBoxes and convert the Text
    >> to
    >> DateTime if not empty.
    >>
    >> My code is :
    >>
    >> Public Shared Function GetDateTime(ByVal DateValue As String) As String
    >>
    >> If Not String.IsNullOrEmpty(DateValue) Then
    >> Return DateTime.Parse(DateValue)
    >> Else
    >> Return Nothing
    >> End If
    >>
    >> End Function
    >>
    >> Then in a OnClick command I have:
    >>
    >> CurrentMember.StartDate = GetDateTime(txtStartDate.Text)
    >>
    >> It works ok when there is a date but if its empty I get an error message:
    >> System.InvalidCastException: Conversion from string "" to type 'Date' is
    >> not
    >> valid.
    >>
    >> If I do CurrentMember.StartDate = Nothing it works fine so no problem
    >> with
    >> Null values in the field.
    >>
    >> Does anyone have any suggestions as this has puzzled me for too long now.
    >>
    >> Thanks in advance
    >> James

    >
    > sorry misread your post
    >
    > Function GetDateTime(ByVal DateValue As String) As String
    > Try
    > Return DateTime.Parse(DateValue)
    > Catch ex As Exception
    > Return Nothing
    > End Try
    > End Function
    >
    sloan, Apr 19, 2008
    #5
  6. "sloan" <> wrote in message
    news:...
    >
    > You should not use Exception catching as a part of normal business flow.


    thats a opinion only, I do not agree with it.




    >
    >
    > http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.aspx
    >
    >
    > I would stick with my TryParse version before I'd use the catch exception
    > version.
    >
    >
    > ..
    >
    >
    >
    >
    >
    > "ThatsIT.net.au" <me@work> wrote in message
    > news:...
    >>
    >> "James" <> wrote in message
    >> news:...
    >>> Hello,
    >>>
    >>> I am trying to create a Function to test TextBoxes and convert the Text
    >>> to
    >>> DateTime if not empty.
    >>>
    >>> My code is :
    >>>
    >>> Public Shared Function GetDateTime(ByVal DateValue As String) As String
    >>>
    >>> If Not String.IsNullOrEmpty(DateValue) Then
    >>> Return DateTime.Parse(DateValue)
    >>> Else
    >>> Return Nothing
    >>> End If
    >>>
    >>> End Function
    >>>
    >>> Then in a OnClick command I have:
    >>>
    >>> CurrentMember.StartDate = GetDateTime(txtStartDate.Text)
    >>>
    >>> It works ok when there is a date but if its empty I get an error
    >>> message:
    >>> System.InvalidCastException: Conversion from string "" to type 'Date' is
    >>> not
    >>> valid.
    >>>
    >>> If I do CurrentMember.StartDate = Nothing it works fine so no problem
    >>> with
    >>> Null values in the field.
    >>>
    >>> Does anyone have any suggestions as this has puzzled me for too long
    >>> now.
    >>>
    >>> Thanks in advance
    >>> James

    >>
    >> sorry misread your post
    >>
    >> Function GetDateTime(ByVal DateValue As String) As String
    >> Try
    >> Return DateTime.Parse(DateValue)
    >> Catch ex As Exception
    >> Return Nothing
    >> End Try
    >> End Function
    >>

    >
    >
    ThatsIT.net.au, Apr 19, 2008
    #6
  7. James

    Guest

    Hi James,

    Apparently, VB.Net converts Nothing to "". Therefore, I suggest you
    declare your method to return a DateTime value as opposed to a string.
    Nothing will then be converted into DateTime.MinValue (01/01/0001) as
    you probably already noticed when you assigned Nothing directly to
    CurrentMember.StartDate.

    But to safeguard against strings that are invalid dates, I would also
    incorporate Try/Catch or TryParse as recommended by the previous
    posters.

    ==========
    Regards,
    Steve
    www.stkomp.com

    On Apr 19, 9:15 am, James <> wrote:
    > Hello,
    >
    > I am trying to create a Function to test TextBoxes and convert the Text to
    > DateTime if not empty.
    >
    > My code is :
    >
    > Public Shared Function GetDateTime(ByVal DateValue As String) As String
    >
    > If Not String.IsNullOrEmpty(DateValue) Then
    > Return DateTime.Parse(DateValue)
    > Else
    > Return Nothing
    > End If
    >
    > End Function
    >
    > Then in a OnClick command I have:
    >
    > CurrentMember.StartDate = GetDateTime(txtStartDate.Text)
    >
    > It works ok when there is a date but if its empty I get an error message:
    > System.InvalidCastException: Conversion from string "" to type 'Date' is not
    > valid.
    >
    > If I do CurrentMember.StartDate = Nothing it works fine so no problem with
    > Null values in the field.
    >
    > Does anyone have any suggestions as this has puzzled me for too long now.
    >
    > Thanks in advance
    > James
    , Apr 19, 2008
    #7
  8. You can use nullable types, if using 2.0, but you will have to test the
    value before you bind it, as Nothing/null blows up when bound to a control.

    As far as conversion, .TryParse() is a safer method of setting a DateTime,
    as users sometimes do not respect dates.

    If you want to force dates, you can make the control so it cannot be filled
    in and use a calendar control. Or you can use an AJAX masked edit (in the
    AJAX control toolkit).

    --
    Gregory A. Beamer
    MVP, MCP: +I, SE, SD, DBA

    Subscribe to my blog
    http://gregorybeamer.spaces.live.com/lists/feed.rss

    or just read it:
    http://gregorybeamer.spaces.live.com/

    *************************************************
    | Think outside the box!
    |
    *************************************************
    "James" <> wrote in message
    news:...
    > Hello,
    >
    > I am trying to create a Function to test TextBoxes and convert the Text to
    > DateTime if not empty.
    >
    > My code is :
    >
    > Public Shared Function GetDateTime(ByVal DateValue As String) As String
    >
    > If Not String.IsNullOrEmpty(DateValue) Then
    > Return DateTime.Parse(DateValue)
    > Else
    > Return Nothing
    > End If
    >
    > End Function
    >
    > Then in a OnClick command I have:
    >
    > CurrentMember.StartDate = GetDateTime(txtStartDate.Text)
    >
    > It works ok when there is a date but if its empty I get an error message:
    > System.InvalidCastException: Conversion from string "" to type 'Date' is
    > not
    > valid.
    >
    > If I do CurrentMember.StartDate = Nothing it works fine so no problem with
    > Null values in the field.
    >
    > Does anyone have any suggestions as this has puzzled me for too long now.
    >
    > Thanks in advance
    > James
    Cowboy \(Gregory A. Beamer\), Apr 19, 2008
    #8
  9. ThatsIT.net.au, Apr 21, 2008
    #9
  10. James

    sloan Guest

    http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand(VS.71).aspx
    That's a try/finally block, not a try/catch or a try/catch/finally block.

    Yes, you're' right, technet is full of try/finally examples.
    But we aren't discussing try/finally blocks, we're discussing try/catch
    blocks.

    ...


    //thats a opinion only, I do not agree with it.//
    Since Brad Abrams and Krzysztof Cwalina have probably forgotten more about
    ..Net then most people know (including myself), I'd probably value their
    opinion more highly.




    http://msdn2.microsoft.com/en-us/library/ms229005.aspx
    http://msdn2.microsoft.com/en-us/library/ms229009.aspx







    "ThatsIT.net.au" <me@work> wrote in message
    news:...
    > "Mark Rae [MVP]" <> wrote in message
    > news:%23%...
    >> "sloan" <> wrote in message
    >> news:...
    >>
    >>> You should not use Exception catching as a part of normal business flow.
    >>> http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.aspx

    >>
    >> Agreed 100%.
    >>
    >>

    >
    > Technet is full of examples of doing just that
    >
    > http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand(VS.71).aspx
    >
    >
    >
    >> --
    >> Mark Rae
    >> ASP.NET MVP
    >> http://www.markrae.net

    >
    sloan, Apr 21, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael

    Function to do nothing

    Michael, Nov 16, 2004, in forum: C++
    Replies:
    9
    Views:
    347
    Old Wolf
    Nov 16, 2004
  2. Eric Lilja
    Replies:
    4
    Views:
    366
    Shezan Baig
    Feb 28, 2005
  3. Devin Jeanpierre
    Replies:
    2
    Views:
    431
    Devin Jeanpierre
    Feb 14, 2012
  4. Gunnar Strand

    sub returning nothing

    Gunnar Strand, Jun 8, 2004, in forum: Perl Misc
    Replies:
    13
    Views:
    159
    Anno Siegel
    Jun 10, 2004
  5. furby
    Replies:
    5
    Views:
    143
    furby
    Sep 18, 2006
Loading...

Share This Page