472,328 Members | 1,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 14951
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Joel Moore | last post by:
Maybe I'm just easily baffled after an all-nighter but I can't seem to figure out how to represent a BitArray as a hexadecimal string. For...
1
by: phancey | last post by:
I am trying to invoke a web service method dynamically. I have created a generic function that takes a method name, string of parameters and calls...
3
by: Tim Osborne | last post by:
I want to know if a call to Convert.Int32 can handle a Culture specific string. I have the following code: string sText =...
3
by: Convert TextBox.Text to Int32 Problem | last post by:
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. ...
5
by: Andy Sutorius | last post by:
Hi, I am attempting to convert this vb function to csharp but I am getting stuck on the if statement dt.Rows(iLoop)("FAQCategoryID")). The...
3
by: James | last post by:
Has anyone written a utility to convert a C# form to C++.net? i.e. to convert "using System.Data" to "using namespace System::Data" etc
3
by: Gee | last post by:
Hi Can anyone help me convert this to C# please? Structure NETRESOURCE Public dwScope As Int32 Public dwType As Int32 Public dwDisplayType...
8
by: Gee | last post by:
I get the above error with this code and I can't figure out why? Any ideas please? See code below - the actual error is included in the code: ...
2
by: Chris | last post by:
Hi, Can someone convert this to VB.NET Please. I am trying but for some reason it doesn't work. if (null != Request.QueryString) {...
14
by: Me | last post by:
Hi all I am getting a really bizzare error on when I convert a string into a datetime: The code is : DateTime dt1 = Convert.ToDateTime("10...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.