By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,969 Members | 2,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,969 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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...es/104504.aspx
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Convert TextBox.Text to Int32 Problem" <ch**********@centerpointenergy.com>
wrote in message news:1F**********************************@microsof t.com...
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!

Nov 16 '05 #2

P: n/a
Convert TextBox.Text to Int32 Problem
<ch**********@centerpointenergy.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

P: n/a
I went to the weblog.asp.net site. It looks great, how do I compile the library, are there any instructions?
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.