Returning Nothing from Function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?SmFtZXM=?=

    Returning Nothing from Function

    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(ByV al DateValue As String) As String

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

    End Function

    Then in a OnClick command I have:

    CurrentMember.S tartDate = GetDateTime(txt StartDate.Text)

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

    If I do CurrentMember.S tartDate = 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

    #2
    Re: Returning Nothing from Function

    You can try this.


    Public Shared Function GetDateTime(ByV al DateValue As String) As String

    dim returnValue as DateTime = DateTime.MinVal ue

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


    End If


    if ( returnValue = DateTime.MinVal ue ) then
    return Nothing
    end if

    return returnValue


    End Function


    Or research "nullable".





    "James" <James@discussi ons.microsoft.c omwrote in message
    news:8192FA1E-C2C8-4845-9E3F-516F9A5ED65A@mi crosoft.com...
    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(ByV al DateValue As String) As String
    >
    If Not String.IsNullOr Empty(DateValue ) Then
    Return DateTime.Parse( DateValue)
    Else
    Return Nothing
    End If
    >
    End Function
    >
    Then in a OnClick command I have:
    >
    CurrentMember.S tartDate = GetDateTime(txt StartDate.Text)
    >
    It works ok when there is a date but if its empty I get an error message:
    System.InvalidC astException: Conversion from string "" to type 'Date' is
    not
    valid.
    >
    If I do CurrentMember.S tartDate = 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

    Comment

    • ThatsIT.net.au

      #3
      Re: Returning Nothing from Function


      "James" <James@discussi ons.microsoft.c omwrote in message
      news:8192FA1E-C2C8-4845-9E3F-516F9A5ED65A@mi crosoft.com...
      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(ByV al DateValue As String) As String
      >
      If Not String.IsNullOr Empty(DateValue ) Then
      Return DateTime.Parse( DateValue)
      Else
      Return Nothing
      End If
      >
      End Function
      >
      Then in a OnClick command I have:
      >
      CurrentMember.S tartDate = GetDateTime(txt StartDate.Text)
      >
      It works ok when there is a date but if its empty I get an error message:
      System.InvalidC astException: Conversion from string "" to type 'Date' is
      not
      valid.
      >
      If I do CurrentMember.S tartDate = 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.IsNullOr Empty(DateValue ) AND DateValue <String.Empty Then

      Comment

      • ThatsIT.net.au

        #4
        Re: Returning Nothing from Function


        "James" <James@discussi ons.microsoft.c omwrote in message
        news:8192FA1E-C2C8-4845-9E3F-516F9A5ED65A@mi crosoft.com...
        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(ByV al DateValue As String) As String
        >
        If Not String.IsNullOr Empty(DateValue ) Then
        Return DateTime.Parse( DateValue)
        Else
        Return Nothing
        End If
        >
        End Function
        >
        Then in a OnClick command I have:
        >
        CurrentMember.S tartDate = GetDateTime(txt StartDate.Text)
        >
        It works ok when there is a date but if its empty I get an error message:
        System.InvalidC astException: Conversion from string "" to type 'Date' is
        not
        valid.
        >
        If I do CurrentMember.S tartDate = 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(ByV al DateValue As String) As String
        Try
        Return DateTime.Parse( DateValue)
        Catch ex As Exception
        Return Nothing
        End Try
        End Function

        Comment

        • sloan

          #5
          Re: Returning Nothing from Function


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





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


          ...





          "ThatsIT.net.au " <me@workwrote in message
          news:784FB9FB-E666-405A-A94F-DCC29935AA91@mi crosoft.com...
          >
          "James" <James@discussi ons.microsoft.c omwrote in message
          news:8192FA1E-C2C8-4845-9E3F-516F9A5ED65A@mi crosoft.com...
          >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(ByV al DateValue As String) As String
          >>
          > If Not String.IsNullOr Empty(DateValue ) Then
          > Return DateTime.Parse( DateValue)
          > Else
          > Return Nothing
          > End If
          >>
          > End Function
          >>
          >Then in a OnClick command I have:
          >>
          >CurrentMember. StartDate = GetDateTime(txt StartDate.Text)
          >>
          >It works ok when there is a date but if its empty I get an error message:
          >System.Invalid CastException: Conversion from string "" to type 'Date' is
          >not
          >valid.
          >>
          >If I do CurrentMember.S tartDate = 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(ByV al DateValue As String) As String
          Try
          Return DateTime.Parse( DateValue)
          Catch ex As Exception
          Return Nothing
          End Try
          End Function
          >

          Comment

          • wisccal@googlemail.com

            #6
            Re: Returning Nothing from Function

            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.MinVal ue (01/01/0001) as
            you probably already noticed when you assigned Nothing directly to
            CurrentMember.S tartDate.

            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


            On Apr 19, 9:15 am, James <Ja...@discussi ons.microsoft.c omwrote:
            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(ByV al DateValue As String) As String
            >
            If Not String.IsNullOr Empty(DateValue ) Then
            Return DateTime.Parse( DateValue)
            Else
            Return Nothing
            End If
            >
            End Function
            >
            Then in a OnClick command I have:
            >
            CurrentMember.S tartDate = GetDateTime(txt StartDate.Text)
            >
            It works ok when there is a date but if its empty I get an error message:
            System.InvalidC astException: Conversion from string "" to type 'Date' is not
            valid.
            >
            If I do CurrentMember.S tartDate = 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

            Comment

            • Cowboy \(Gregory A. Beamer\)

              #7
              Re: Returning Nothing from Function

              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


              or just read it:


              *************** *************** *************** ****
              | Think outside the box!
              |
              *************** *************** *************** ****
              "James" <James@discussi ons.microsoft.c omwrote in message
              news:8192FA1E-C2C8-4845-9E3F-516F9A5ED65A@mi crosoft.com...
              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(ByV al DateValue As String) As String
              >
              If Not String.IsNullOr Empty(DateValue ) Then
              Return DateTime.Parse( DateValue)
              Else
              Return Nothing
              End If
              >
              End Function
              >
              Then in a OnClick command I have:
              >
              CurrentMember.S tartDate = GetDateTime(txt StartDate.Text)
              >
              It works ok when there is a date but if its empty I get an error message:
              System.InvalidC astException: Conversion from string "" to type 'Date' is
              not
              valid.
              >
              If I do CurrentMember.S tartDate = 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

              Comment

              • ThatsIT.net.au

                #8
                Re: Returning Nothing from Function


                "sloan" <sloan@ipass.ne twrote in message
                news:uUu6CdkoIH A.1164@TK2MSFTN GP04.phx.gbl...
                >
                You should not use Exception catching as a part of normal business flow.
                thats a opinion only, I do not agree with it.



                >
                >

                >
                >
                I would stick with my TryParse version before I'd use the catch exception
                version.
                >
                >
                ..
                >
                >
                >
                >
                >
                "ThatsIT.net.au " <me@workwrote in message
                news:784FB9FB-E666-405A-A94F-DCC29935AA91@mi crosoft.com...
                >>
                >"James" <James@discussi ons.microsoft.c omwrote in message
                >news:8192FA1 E-C2C8-4845-9E3F-516F9A5ED65A@mi crosoft.com...
                >>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(ByV al DateValue As String) As String
                >>>
                >> If Not String.IsNullOr Empty(DateValue ) Then
                >> Return DateTime.Parse( DateValue)
                >> Else
                >> Return Nothing
                >> End If
                >>>
                >> End Function
                >>>
                >>Then in a OnClick command I have:
                >>>
                >>CurrentMember .StartDate = GetDateTime(txt StartDate.Text)
                >>>
                >>It works ok when there is a date but if its empty I get an error
                >>message:
                >>System.Invali dCastException: Conversion from string "" to type 'Date' is
                >>not
                >>valid.
                >>>
                >>If I do CurrentMember.S tartDate = 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(ByV al DateValue As String) As String
                > Try
                > Return DateTime.Parse( DateValue)
                > Catch ex As Exception
                > Return Nothing
                > End Try
                > End Function
                >>
                >
                >

                Comment

                • Mark Rae [MVP]

                  #9
                  Re: Returning Nothing from Function

                  "sloan" <sloan@ipass.ne twrote in message
                  news:uUu6CdkoIH A.1164@TK2MSFTN GP04.phx.gbl...
                  You should not use Exception catching as a part of normal business flow.
                  http://blogs.msdn.com/kcwalina/archi...16/396787.aspx
                  Agreed 100%.


                  --
                  Mark Rae
                  ASP.NET MVP


                  Comment

                  • ThatsIT.net.au

                    #10
                    Re: Returning Nothing from Function

                    "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
                    news:%23%238Kqz voIHA.4716@TK2M SFTNGP06.phx.gb l...
                    "sloan" <sloan@ipass.ne twrote in message
                    news:uUu6CdkoIH A.1164@TK2MSFTN GP04.phx.gbl...
                    >
                    >You should not use Exception catching as a part of normal business flow.
                    >http://blogs.msdn.com/kcwalina/archi...16/396787.aspx
                    >
                    Agreed 100%.
                    >
                    >
                    Technet is full of examples of doing just that

                    Represents a Transact-SQL statement or stored procedure to execute against a SQL Server database. This class cannot be inherited.



                    --
                    Mark Rae
                    ASP.NET MVP
                    http://www.markrae.net

                    Comment

                    • sloan

                      #11
                      Re: Returning Nothing from Function

                      Represents a Transact-SQL statement or stored procedure to execute against a SQL Server database. This class cannot be inherited.

                      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.













                      "ThatsIT.net.au " <me@workwrote in message
                      news:0502FA77-0E67-441D-A328-F818D8A50B9C@mi crosoft.com...
                      "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
                      news:%23%238Kqz voIHA.4716@TK2M SFTNGP06.phx.gb l...
                      >"sloan" <sloan@ipass.ne twrote in message
                      >news:uUu6CdkoI HA.1164@TK2MSFT NGP04.phx.gbl.. .
                      >>
                      >>You should not use Exception catching as a part of normal business flow.
                      >>http://blogs.msdn.com/kcwalina/archi...16/396787.aspx
                      >>
                      >Agreed 100%.
                      >>
                      >>
                      >
                      Technet is full of examples of doing just that
                      >
                      Represents a Transact-SQL statement or stored procedure to execute against a SQL Server database. This class cannot be inherited.

                      >
                      >
                      >
                      >--
                      >Mark Rae
                      >ASP.NET MVP
                      >http://www.markrae.net
                      >

                      Comment

                      Working...