Can't convert Text string to an int32 using Convert.Int32

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Convert TextBox.Text to Int32 Problem

    Can't convert Text string to an int32 using Convert.Int32

    Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function and there you have it. So I enquired and found the Convert class with it's promising ToInt32 method, great... but it doesn't work. The thing keeps throwing Format Exceptions all over the place. What is the "C#" way to do this??? code

    int wmin, wsec, x
    int hrs, min, sec, miles
    miles = Convert.ToInt32 (txtMiles.text)
    hrs = Convert.ToInt32 (txtHours.text)
    min = Convert.ToInt32 (txtMinutes.tex t)
    sec = Convert.ToInt32 (txtSeconds.tex t)
    . .

    I even tried forcing the values to string by using .... miles = Convert.ToInt32 (txtMiles.text. ToString())
    still not working...
    Help!
  • Justin Rogers

    #2
    Re: Can't convert Text string to an int32 using Convert.Int32

    The property should be .Text for one. And if the conversion can't take place a
    FormatException
    is expected. You can try using double.TryParse instead and converting to an
    integer. There are
    other methods as well (Whidbey has an int.TryParse), and I've developed a
    library that does
    what you are looking for:




    --
    Justin Rogers
    DigiTec Web Consultants, LLC.
    Blog: http://weblogs.asp.net/justin_rogers

    "Convert TextBox.Text to Int32 Problem" <chris.stueck@c enterpointenerg y.com>
    wrote in message news:1F83AE18-14D7-4516-9412-684C829D32B6@mi crosoft.com...[color=blue]
    > Need a little help here. I saw some related posts, so here goes... I have[/color]
    some textboxes which are designed for the user to enter a integer value. In
    "old school C" we just used the atoi function and there you have it. So I
    enquired and found the Convert class with it's promising ToInt32 method,
    great... but it doesn't work. The thing keeps throwing Format Exceptions all
    over the place. What is the "C#" way to do this??? code:[color=blue]
    >
    > int wmin, wsec, x;
    > int hrs, min, sec, miles;
    > miles = Convert.ToInt32 (txtMiles.text) ;
    > hrs = Convert.ToInt32 (txtHours.text) ;
    > min = Convert.ToInt32 (txtMinutes.tex t);
    > sec = Convert.ToInt32 (txtSeconds.tex t);
    > . . .
    >
    > I even tried forcing the values to string by using .... miles =[/color]
    Convert.ToInt32 (txtMiles.text. ToString());[color=blue]
    > still not working....
    > Help![/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Can't convert Text string to an int32 using Convert.Int32

      Convert TextBox.Text to Int32 Problem
      <chris.stueck@c enterpointenerg y.com> wrote:
      [color=blue]
      > Need a little help here. I saw some related posts, so here goes... I
      > have some textboxes which are designed for the user to enter a
      > integer value. In "old school C" we just used the atoi function and
      > there you have it. So I enquired and found the Convert class with
      > it's promising ToInt32 method, great... but it doesn't work. The
      > thing keeps throwing Format Exceptions all over the place. What is
      > the "C#" way to do this??? code:
      >
      > int wmin, wsec, x;
      > int hrs, min, sec, miles;
      > miles = Convert.ToInt32 (txtMiles.text) ;
      > hrs = Convert.ToInt32 (txtHours.text) ;
      > min = Convert.ToInt32 (txtMinutes.tex t);
      > sec = Convert.ToInt32 (txtSeconds.tex t);
      > . . .[/color]

      If Convert.ToInt32 (string) is throwing an exception, then your text
      presumably isn't a valid integer. Note that unlike atoi, which stops
      when it meets the first non-numeric character and returns whatever it's
      parsed so far, Convert.ToInt32 (string) and Int32.Parse both require the
      whole string to be a valid integer value (although whitespace is
      allowed, by default).

      If you want to emulate atoi, you'll have to write a method to find the
      leading substring of a string which is composed of digits (perhaps
      prefixed with '-'), or "0" if such a string would have no digits in.
      [color=blue]
      > I even tried forcing the values to string by using .... miles =
      > Convert.ToInt32 (txtMiles.text. ToString());[/color]

      If it wasn't a string before, what was it? I would imagine it was
      already a string - calling ToString() on a string isn't going to make
      any difference.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Chris Stueck

        #4
        Re: Can't convert Text string to an int32 using Convert.Int32

        I went to the weblog.asp.net site. It looks great, how do I compile the library, are there any instructions?

        Comment

        Working...