try Catch for empty textfield

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Devlon

    try Catch for empty textfield

    Hi,

    I would like to check if a text field is empty; I'm using this code ...

    Dim strTitle As String = String.Empty

    Try
    strTitle = Trim(txtTitle.T ext)
    Catch ex As Exception When strTitle = String.Empty
    MessageBox.Show ("error")
    End Try


    unfortunately, the exception doesn't work...

    Anyone any idea's ?

    Thanx

    john



  • Stuart Nathan

    #2
    Re: try Catch for empty textfield

    try =""


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: try Catch for empty textfield

      "John Devlon" <johndevlon@hot mail.comschrieb :
      I would like to check if a text field is empty; I'm using this code ...
      >
      Dim strTitle As String = String.Empty
      >
      Try
      strTitle = Trim(txtTitle.T ext)
      Catch ex As Exception When strTitle = String.Empty
      MessageBox.Show ("error")
      End Try
      >
      >
      unfortunately, the exception doesn't work...
      'Trim' does not throw an exception.

      \\\
      If Len(Trim(Me.Tex tBoxTitle.Text) ) = 0 Then
      ...
      End If
      ///

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

      Comment

      • Oenone

        #4
        Re: try Catch for empty textfield

        John Devlon wrote:
        I would like to check if a text field is empty; I'm using this code
        [...]
        unfortunately, the exception doesn't work...
        That's because reading and trimming an empty string from a textbox doesn't
        throw an exception.

        Try this:

        \\\
        Dim strTitle As String = String.Empty

        strTitle = Trim(txtTitle.T ext)
        If Len(strTitle) = 0 Then
        MessageBox.Show ("error")
        End If
        ///

        --

        (O)enone


        Comment

        • Jim Wooley

          #5
          Re: try Catch for empty textfield

          "John Devlon" <johndevlon@hot mail.comschrieb :
          >
          >I would like to check if a text field is empty; I'm using this code
          >...
          >>
          >Dim strTitle As String = String.Empty
          >>
          >Try
          >strTitle = Trim(txtTitle.T ext)
          >Catch ex As Exception When strTitle = String.Empty
          >MessageBox.Sho w("error")
          >End Try
          >unfortunatel y, the exception doesn't work...
          >>
          'Trim' does not throw an exception.
          >
          \\\
          If Len(Trim(Me.Tex tBoxTitle.Text) ) = 0 Then
          ...
          End If
          ///
          Actuall, String.Trim can throw an exception. Consider the following:
          Dim foo As String
          Console.WriteLi ne(foo.Trim)

          In this case foo is not instanced and thus we try to Trim Nothing. Since
          string is an object not a value type, it is not initiated by default and
          thus can be null/nothing. Because of this, I always check for nothing on
          my strings passed into to methods as parameters before processing them. (if
          value = nothing then value = string.Empty). This is particularly noticable
          when binding a Combobox's SelectedValue to string property of an object.

          You are correct that the TextBox.Text can not pass back nothing, thus in
          the OP's code, it will not throw an exception. Furthermore, relying on exceptions
          when you can pre-test for a condition is akin to peeing your pants to see
          if the fly is unzipped. Thus, checking to see if txtTitle.Text.T rim=String.Empt y
          is much better than using exceptions and try..catch blocks anyway.

          Jim Wooley



          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: try Catch for empty textfield

            "Jim Wooley" <jimNOSPAMwoole y@hotmail.comsc hrieb:
            >>I would like to check if a text field is empty; I'm using this code
            >>...
            >>>
            >>Dim strTitle As String = String.Empty
            >>>
            >>Try
            >>strTitle = Trim(txtTitle.T ext)
            >>Catch ex As Exception When strTitle = String.Empty
            >>MessageBox.Sh ow("error")
            >>End Try
            >>unfortunately , the exception doesn't work...
            >>>
            >'Trim' does not throw an exception.
            >>
            >\\\
            >If Len(Trim(Me.Tex tBoxTitle.Text) ) = 0 Then
            >...
            >End If
            >///
            >
            Actuall, String.Trim can throw an exception. Consider the following:
            Dim foo As String
            Console.WriteLi ne(foo.Trim)
            That's true, but I am using 'Microsoft.Visu alBasic.Strings .Trim' instead of
            'String.Trim'. Instead of performing the check if the variable containing
            the string to be trimmed is 'Nothing' I delegate this check to 'Trim'.

            --
            M S Herfried K. Wagner
            M V P <URL:http://dotnet.mvps.org/>
            V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

            Comment

            • Greg

              #7
              Re: try Catch for empty textfield

              "John Devlon" <johndevlon@hot mail.comwrote in message
              news:7OUIg.4763 3$a6.711718@pho bos.telenet-ops.be...
              Hi,
              >
              I would like to check if a text field is empty; I'm using this code ...
              >
              Dim strTitle As String = String.Empty
              >
              Try
              strTitle = Trim(txtTitle.T ext)
              Catch ex As Exception When strTitle = String.Empty
              MessageBox.Show ("error")
              End Try
              >
              >
              unfortunately, the exception doesn't work...
              >
              Anyone any idea's ?
              >
              Thanx
              >
              john
              If Trim(txtTitle.T ext) = "" Then MsgBox ("error")

              Cheers.


              Comment

              • Dennis

                #8
                Re: try Catch for empty textfield

                I would use:

                If not txtTitle.Text is nothing andalso txtTitle.Text.T rim.Length<=0 then
                'do something if true
                end if
                --
                Dennis in Houston


                "Greg" wrote:
                "John Devlon" <johndevlon@hot mail.comwrote in message
                news:7OUIg.4763 3$a6.711718@pho bos.telenet-ops.be...
                Hi,

                I would like to check if a text field is empty; I'm using this code ...

                Dim strTitle As String = String.Empty

                Try
                strTitle = Trim(txtTitle.T ext)
                Catch ex As Exception When strTitle = String.Empty
                MessageBox.Show ("error")
                End Try


                unfortunately, the exception doesn't work...

                Anyone any idea's ?

                Thanx

                john
                >
                If Trim(txtTitle.T ext) = "" Then MsgBox ("error")
                >
                Cheers.
                >
                >
                >

                Comment

                • Al Reid

                  #9
                  Re: try Catch for empty textfield

                  "Dennis" <Dennis@discuss ions.microsoft. comwrote in message news:243F095E-8052-4AD9-B591-C8C9862C4D3F@mi crosoft.com...
                  I would use:
                  >
                  If not txtTitle.Text is nothing andalso txtTitle.Text.T rim.Length<=0 then
                  'do something if true
                  end if
                  --
                  Dennis in Houston
                  >
                  >
                  Dennis,

                  Can you show me a scenario where the .Text property of a TextBox can contain Nothing. I can't find one.

                  I have put the following in a button click event:

                  If TextBox1.Text Is Nothing Then

                  MsgBox("Textbox 1.Text is Nothing")

                  Else

                  TextBox1.Text = Nothing

                  End If



                  I can click on the TextBox as many times as I want and I never get the message box.



                  BTW, I'm using VB.Net 2005.



                  --

                  Al Reid


                  Comment

                  • Dennis

                    #10
                    Re: try Catch for empty textfield

                    You are correct but whenever I check for a string's length, etc., I always
                    check for nothing first...I think it's good practice to get in this habit and
                    avoid unexpected errors since a lot of softwre doesn't check for boundaries
                    like variables set to nothing.
                    --
                    Dennis in Houston


                    "Al Reid" wrote:
                    "Dennis" <Dennis@discuss ions.microsoft. comwrote in message news:243F095E-8052-4AD9-B591-C8C9862C4D3F@mi crosoft.com...
                    I would use:

                    If not txtTitle.Text is nothing andalso txtTitle.Text.T rim.Length<=0 then
                    'do something if true
                    end if
                    --
                    Dennis in Houston
                    >
                    Dennis,
                    >
                    Can you show me a scenario where the .Text property of a TextBox can contain Nothing. I can't find one.
                    >
                    I have put the following in a button click event:
                    >
                    If TextBox1.Text Is Nothing Then
                    >
                    MsgBox("Textbox 1.Text is Nothing")
                    >
                    Else
                    >
                    TextBox1.Text = Nothing
                    >
                    End If
                    >
                    >
                    >
                    I can click on the TextBox as many times as I want and I never get the message box.
                    >
                    >
                    >
                    BTW, I'm using VB.Net 2005.
                    >
                    >
                    >
                    --
                    >
                    Al Reid
                    >
                    >
                    >

                    Comment

                    Working...