Conversion from string "" to type 'Integer' is not valid.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave griffiths

    #1

    Conversion from string "" to type 'Integer' is not valid.

    Hi all
    Using VB2005 on Vista with a Norwegian locale setup.

    The test program has 3 textboxes the sum held in txt3.

    Using the code below, txt2 conversion causes an error when it is left
    empty. The code in txt1 works OK but I am asking is there a better way
    of coding this

    Public Class Main
    Dim y As Integer
    Dim x As Integer
    Private Sub txt1_LostFocus( ByVal sender As Object, ByVal e As
    System.EventArg s) Handles txt1.LostFocus

    If txt1.Text = "" Then
    y = 0
    Else
    y = CInt(txt1.Text)
    End If
    End Sub

    Private Sub txt2_LostFocus( ByVal sender As Object, ByVal e As
    System.EventArg s) Handles txt2.LostFocus

    ' This produces an error if txt2 is empty
    x = CInt(txt2.Text)

    txt3.Text = x + y
    End Sub
    End Class

    Thanks in advance.


    --
    Dave Griffiths
  • rowe_newsgroups

    #2
    Re: Conversion from string "" to type 'Integer' is not valid.

    On Jul 2, 10:30 am, "Dave griffiths" <davegino...@no spam.hotmail.co m>
    wrote:
    Hi all
    Using VB2005 on Vista with a Norwegian locale setup.
    >
    The test program has 3 textboxes the sum held in txt3.
    >
    Using the code below, txt2 conversion causes an error when it is left
    empty. The code in txt1 works OK but I am asking is there a better way
    of coding this
    >
    Public Class Main
    Dim y As Integer
    Dim x As Integer
    Private Sub txt1_LostFocus( ByVal sender As Object, ByVal e As
    System.EventArg s) Handles txt1.LostFocus
    >
    If txt1.Text = "" Then
    y = 0
    Else
    y = CInt(txt1.Text)
    End If
    End Sub
    >
    Private Sub txt2_LostFocus( ByVal sender As Object, ByVal e As
    System.EventArg s) Handles txt2.LostFocus
    >
    ' This produces an error if txt2 is empty
    x = CInt(txt2.Text)
    >
    txt3.Text = x + y
    End Sub
    End Class
    >
    Thanks in advance.
    >
    --
    Dave Griffiths
    I would do this:

    If IsNumeric(txt2. Text) Then
    x = Integer.Parse(t xt2.Text)
    Else
    x = 0
    End If

    Or Even this:

    Dim x as Integer = 0
    Integer.TryPars e(txt2.Text, x)

    Thanks,

    Seth Rowe

    Comment

    • rowe_newsgroups

      #3
      Re: Conversion from string &quot;&quot; to type 'Integer' is not valid.

      On Jul 2, 10:42 am, rowe_newsgroups <rowe_em...@yah oo.comwrote:
      On Jul 2, 10:30 am, "Dave griffiths" <davegino...@no spam.hotmail.co m>
      wrote:
      >
      >
      >
      Hi all
      Using VB2005 on Vista with a Norwegian locale setup.
      >
      The test program has 3 textboxes the sum held in txt3.
      >
      Using the code below, txt2 conversion causes an error when it is left
      empty. The code in txt1 works OK but I am asking is there a better way
      of coding this
      >
      Public Class Main
      Dim y As Integer
      Dim x As Integer
      Private Sub txt1_LostFocus( ByVal sender As Object, ByVal e As
      System.EventArg s) Handles txt1.LostFocus
      >
      If txt1.Text = "" Then
      y = 0
      Else
      y = CInt(txt1.Text)
      End If
      End Sub
      >
      Private Sub txt2_LostFocus( ByVal sender As Object, ByVal e As
      System.EventArg s) Handles txt2.LostFocus
      >
      ' This produces an error if txt2 is empty
      x = CInt(txt2.Text)
      >
      txt3.Text = x + y
      End Sub
      End Class
      >
      Thanks in advance.
      >
      --
      Dave Griffiths
      >
      I would do this:
      >
      If IsNumeric(txt2. Text) Then
      x = Integer.Parse(t xt2.Text)
      Else
      x = 0
      End If
      >
      Or Even this:
      >
      Dim x as Integer = 0
      Integer.TryPars e(txt2.Text, x)
      >
      Thanks,
      >
      Seth Rowe
      I forgot to say why :-)

      Using IsNumeric or TryCast will not only prevent a null string from
      popping an exception, but will also stop non-integer values from
      causing problems. For example, if the user enters text into the box
      (you should probably disable this) or if they enter a value that is
      out of range of an integer variable (though I don't think IsNumeric
      will catch this, so you might go the tryparse way)

      Thanks,

      Seth Rowe

      Comment

      • Tim Patrick

        #4
        Re: Conversion from string &quot;&quot; to type 'Integer' is not valid.

        In the txt2_LostFocus event handler, you can change the assignment of "x" to:

        x = CInt(Val(txt2.T ext))

        although some programmers consider Val to be an unsafe alternative to actual
        formal checking of the data. Also, there are some minor situations where
        Val will generate an error instead of returning zero for unrecogized input.

        -----
        Tim Patrick - www.timaki.com
        Start-to-Finish Visual Basic 2005
        Hi all Using VB2005 on Vista with a Norwegian locale setup.
        >
        The test program has 3 textboxes the sum held in txt3.
        >
        Using the code below, txt2 conversion causes an error when it is left
        empty. The code in txt1 works OK but I am asking is there a better way
        of coding this
        >
        Public Class Main
        Dim y As Integer
        Dim x As Integer
        Private Sub txt1_LostFocus( ByVal sender As Object, ByVal e As
        System.EventArg s) Handles txt1.LostFocus
        If txt1.Text = "" Then
        y = 0
        Else
        y = CInt(txt1.Text)
        End If
        End Sub
        Private Sub txt2_LostFocus( ByVal sender As Object, ByVal e As
        System.EventArg s) Handles txt2.LostFocus
        >
        ' This produces an error if txt2 is empty
        x = CInt(txt2.Text)
        txt3.Text = x + y
        End Sub
        End Class
        Thanks in advance.
        >

        Comment

        • Rory Becker

          #5
          Re: Conversion from string &quot;&quot; to type 'Integer' is not valid.

          Hi all Using VB2005 on Vista with a Norwegian locale setup.
          >
          The test program has 3 textboxes the sum held in txt3.
          >
          Using the code below, txt2 conversion causes an error when it is left
          empty. The code in txt1 works OK but I am asking is there a better way
          of coding this
          >
          Public Class Main
          Dim y As Integer
          Dim x As Integer
          Private Sub txt1_LostFocus( ByVal sender As Object, ByVal e As
          System.EventArg s) Handles txt1.LostFocus
          If txt1.Text = "" Then
          y = 0
          Else
          y = CInt(txt1.Text)
          End If
          End Sub
          Private Sub txt2_LostFocus( ByVal sender As Object, ByVal e As
          System.EventArg s) Handles txt2.LostFocus
          >
          ' This produces an error if txt2 is empty
          x = CInt(txt2.Text)
          txt3.Text = x + y
          End Sub
          End Class
          Thanks in advance.
          >


          Well in a pinch you can use....
          -------------------------------------------------------------
          txt3.text = cint(iif(txt2.t ext = "", 0, txt2.text)) + y
          -------------------------------------------------------------
          .... but at this point I would suggest that the proper way to do this would
          be to create som sort of validation routine...

          -------------------------------------------------------------
          Public function ValidateForm(Er rorMessage as String) as Boolean
          -------------------------------------------------------------



          Comment

          • Rory Becker

            #6
            Re: Conversion from string &quot;&quot; to type 'Integer' is not valid.

            x = CInt(txt2.Text)
            txt3.Text = x + y
            My first reply seems to have mysteriously disappeared. so I will try again :)

            Essentially I agree with Seth.

            I would however suggest that for anything that might eventually be a less
            than trivial form, that you should consider the creation of a function to
            validate the form itself before proceeding to perform calculations based
            on the data in that form.

            for example
            -------------------------------------------------------------
            Public Function isFormValid(ByR ef ErrorMessage as String) as Boolean
            If SomeBadConditio n then
            ErrorMessage = "Alert User of Problem"
            Exit Sub
            End If
            If SomeOtherBadCon dition then
            ErrorMessage = "Alert User of Problem2"
            Exit Sub
            End If

            Return True
            End Function
            -------------------------------------------------------------

            Later on you can say...
            -------------------------------------------------------------
            Public Sub PerformCalculat ion
            ' check form first
            Dim TheErrorMessage as String
            If Not isFormValid(The PotentialErrorM essage) then
            MessageBox.Show (TheErrorMessag e)
            Exit Sub
            End If
            ' Now perform calculation
            ....
            ....
            ....
            End Sub
            -------------------------------------------------------------

            This is probably overkill for any small calculation but on larger dataentry
            forms, something like this can save you a lot of trouble.

            --
            Rory



            Comment

            • Dave griffiths

              #7
              Re: Conversion from string &quot;&quot; to type 'Integer' is not valid.

              >Hi Tim

              I have learned to stay away from Val as I live and run my software in a
              non english society.

              From MS
              The Val function recognizes only the period (.) as a valid decimal
              separator. When different decimal separators are used, as in
              international applications, use CDbl or CInt instead to convert a
              string to a number.


              --
              Dave Griffiths


              Tim Patrick wrote:
              In the txt2_LostFocus event handler, you can change the assignment of
              "x" to:
              >
              x = CInt(Val(txt2.T ext))
              >
              although some programmers consider Val to be an unsafe alternative to
              actual formal checking of the data. Also, there are some minor
              situations where Val will generate an error instead of returning zero
              for unrecogized input.
              >
              -----
              Tim Patrick - www.timaki.com
              Start-to-Finish Visual Basic 2005
              >
              Hi all Using VB2005 on Vista with a Norwegian locale setup.

              The test program has 3 textboxes the sum held in txt3.

              Using the code below, txt2 conversion causes an error when it is
              left empty. The code in txt1 works OK but I am asking is there a
              better way of coding this

              Public Class Main
              Dim y As Integer
              Dim x As Integer
              Private Sub txt1_LostFocus( ByVal sender As Object, ByVal e As
              System.EventArg s) Handles txt1.LostFocus
              If txt1.Text = "" Then
              y = 0
              Else
              y = CInt(txt1.Text)
              End If
              End Sub
              Private Sub txt2_LostFocus( ByVal sender As Object, ByVal e As
              System.EventArg s) Handles txt2.LostFocus

              ' This produces an error if txt2 is empty
              x = CInt(txt2.Text)
              txt3.Text = x + y
              End Sub
              End Class
              Thanks in advance.

              Comment

              • Dave griffiths

                #8
                Re: Conversion from string &quot;&quot; to type 'Integer' is not valid.

                Hi All

                Thanks for so much input so quickly.

                I agree with Rory that there needs to be some kind of form validation,
                I was really asking the correct or should I say tidier way of
                performing the conversion.

                As the form uses extensive numeric textboxes I plan to create my own
                class "numtext" with the information kindly supplied here to handle the
                problem.


                --
                Dave Griffiths


                Dave griffiths wrote:
                Hi all
                Using VB2005 on Vista with a Norwegian locale setup.
                >
                The test program has 3 textboxes the sum held in txt3.
                >
                Using the code below, txt2 conversion causes an error when it is left
                empty. The code in txt1 works OK but I am asking is there a better way
                of coding this
                >
                Public Class Main
                Dim y As Integer
                Dim x As Integer
                Private Sub txt1_LostFocus( ByVal sender As Object, ByVal e As
                System.EventArg s) Handles txt1.LostFocus
                >
                If txt1.Text = "" Then
                y = 0
                Else
                y = CInt(txt1.Text)
                End If
                End Sub
                >
                Private Sub txt2_LostFocus( ByVal sender As Object, ByVal e As
                System.EventArg s) Handles txt2.LostFocus
                >
                ' This produces an error if txt2 is empty
                x = CInt(txt2.Text)
                >
                txt3.Text = x + y
                End Sub
                End Class
                >
                Thanks in advance.

                Comment

                • Rory Becker

                  #9
                  Re: Conversion from string &quot;&quot; to type 'Integer' is not valid.

                  As the form uses extensive numeric textboxes I plan to create my own
                  class "numtext" with the information kindly supplied here to handle
                  the problem.
                  If this is a small project then go for it... there's lots of good stuff to
                  be leared by doing it yourself. :)

                  Again though if this is for a large paid project, it might be worth looking
                  into potentially buying in this type of functionality. especially if you
                  see yourself having to deal with multiple different types of formatting.
                  numbers dates

                  I use DeveloperExpres s' XtraEditors but Infragistics and many other companies
                  have reasonably comprehensive solutions to the DataEntry problem as a whole.

                  There are also many good freeware options. www.CodeProject.com should have
                  a few examples.

                  Again it may be overkill. I cannot possibly tell what your situation is Re
                  time or money, but where possible I prefer not to reinvent the wheel and
                  risk making a square one :)

                  --
                  Rory


                  Comment

                  • \(O\)enone

                    #10
                    Re: Conversion from string &quot;&quot; to type 'Integer' is not valid.

                    Dave griffiths wrote:
                    From MS
                    The Val function recognizes only the period (.) as a valid decimal
                    separator. When different decimal separators are used, as in
                    international applications, use CDbl or CInt instead to convert a
                    string to a number.
                    Which sounds great, except that CDbl and CInt don't do the same thing as
                    Val. Val will read numeric data from the start of a non-numeric string and
                    return just the numeric part (so given the string "123ABC", Val will return
                    123.0 whereas CDbl and CInt will raise a type mistmatch; given "Hello World"
                    or "" , Val will return zero as opposed once again to a type mismatch).
                    Sometimes this functionality for Val is extremely useful -- although there
                    are other dangers (Val("1e3") returns 1000.0 rather than 1 as the "e" is
                    interpreted as the exponent operator).

                    We worked around this by writing a replacement function, taking the input
                    value, replacing all "thousands" separators (as defined by the locale) with
                    an empty string, and all "decimal" separators (again as defined by the
                    locale) with a period ("."). Then you can use Val() on it safely.

                    I think it's bizarre that Val works this way, but it does. Humpf.

                    --

                    (O)enone


                    Comment

                    • Dave griffiths

                      #11
                      Re: Conversion from string &quot;&quot; to type 'Integer' is not valid.

                      Thanks Rory

                      This is a small "free" project for a small community, I think I will
                      benefit more by "rolling my own" solution as I know I need the practice.

                      --
                      Dave Griffiths


                      Rory Becker wrote:
                      As the form uses extensive numeric textboxes I plan to create my own
                      class "numtext" with the information kindly supplied here to handle
                      the problem.
                      >
                      If this is a small project then go for it... there's lots of good
                      stuff to be leared by doing it yourself. :)
                      >
                      Again though if this is for a large paid project, it might be worth
                      looking into potentially buying in this type of functionality.
                      especially if you see yourself having to deal with multiple different
                      types of formatting. numbers dates I use DeveloperExpres s'
                      XtraEditors but Infragistics and many other companies have reasonably
                      comprehensive solutions to the DataEntry problem as a whole. There
                      are also many good freeware options. www.CodeProject.com should have
                      a few examples.
                      >
                      Again it may be overkill. I cannot possibly tell what your situation
                      is Re time or money, but where possible I prefer not to reinvent the
                      wheel and risk making a square one :)

                      Comment

                      Working...