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

G

Guest

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.text)
sec = Convert.ToInt32(txtSeconds.text)
. .

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

Justin Rogers

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:

http://weblogs.asp.net/justin_rogers/articles/104504.aspx


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

Convert TextBox.Text to Int32 Problem said:
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:
 
J

Jon Skeet [C# MVP]

Convert TextBox.Text to Int32 Problem
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.text);
sec = Convert.ToInt32(txtSeconds.text);
. . .

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.
I even tried forcing the values to string by using .... miles =
Convert.ToInt32(txtMiles.text.ToString());

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.
 
G

Guest

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top