469,631 Members | 1,171 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

How do I convert a string to Int32?

Doing something like this (cast) doesn't work.

Y.value = (Int32)stringvariable;

I get this message:
Cannot convert type 'string' to 'int'

Any idea how I can convert string to Int32?

thxs
Jeff Reed
Nov 15 '05 #1
6 14840
Jeff Reed wrote:
Any idea how I can convert string to Int32?


int.Parse(string)

There's also the Convert class or double.TryParse. I like TryParse
because it won't throw an exception.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #2
Hi to you,

TryParse is a little bit slower. If you're sure that your string is a
numeric (You can use RegularExpression) then i suggest you Int32.Parse or
int.Parse (same thing)
--
Nivek Eroom

Post message to group, everyones got benefits

"Frank Oquendo" <fr*******@acadx.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Jeff Reed wrote:
Any idea how I can convert string to Int32?


int.Parse(string)

There's also the Convert class or double.TryParse. I like TryParse
because it won't throw an exception.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)

Nov 15 '05 #3
"Frank Oquendo" <fr*******@acadx.com> wrote in message news:#Y*************@TK2MSFTNGP11.phx.gbl...
There's also the Convert class or double.TryParse.


You might still get an error converting the double to an int. It would be fairly easy to write a TryParse function for int, or
create a function that does the conversion and returns 0 if there is an error (or a default value).

--
Michael Culley
Nov 15 '05 #4
Michael Culley wrote:
You might still get an error converting the double to an int. It
would be fairly easy to write a TryParse function for int, or create
a function that does the conversion and returns 0 if there is an
error (or a default value).


TryParse enver throws an exeption. Instead it will return false and set
the out parameter to 0. You can also specify NumberStyles.Integer as an
argument. That will help avoid any casting errors. I use TryParse all
the time and have never had a problem with using to convert strings to
ints.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #5
I didn't know about the second parameter. However, if you create your own function you don't require a cast or to type the second
param. Most the time in my app I just do Gen.ToInt32(x) where Gen is a class I use for common functions.

--
Michael Culley
"Frank Oquendo" <fr*******@acadx.com> wrote in message news:uo**************@tk2msftngp13.phx.gbl...
Michael Culley wrote:
You might still get an error converting the double to an int. It
would be fairly easy to write a TryParse function for int, or create
a function that does the conversion and returns 0 if there is an
error (or a default value).


TryParse enver throws an exeption. Instead it will return false and set
the out parameter to 0. You can also specify NumberStyles.Integer as an
argument. That will help avoid any casting errors. I use TryParse all
the time and have never had a problem with using to convert strings to
ints.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)

Nov 15 '05 #6
Michael Culley wrote:
I didn't know about the second parameter.
There's more than that as well. Another parameter is NumberFormatInfo
object. I normally pass
System.Globalization.CultureInfo.CurrentCulture.Nu mberFormat. Between
that and the NumberStyles parameter, you can parse all manner of strings
such as 10000, 10,000 or even $10,000 all with a single line of code.
However, if you create your
own function you don't require a cast or to type the second param.


Yep, a simple wrapper does come in handy if you don't want to perform a
cast from double to int.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Joel Moore | last post: by
3 posts views Thread by Tim Osborne | last post: by
3 posts views Thread by Convert TextBox.Text to Int32 Problem | last post: by
5 posts views Thread by Andy Sutorius | last post: by
3 posts views Thread by James | last post: by
3 posts views Thread by Gee | last post: by
2 posts views Thread by Chris | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.