473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 15013
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 example: Dim outputBank As New BitArray(8) ...
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 the web method using System.Reflection: ...
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 = "111,111,111"; int i; i = Convert.Int32(...
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. In "old school C" we just used the atoi function...
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 compiler says "method name expected" and underlines...
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 As Int32 Public dwUsage As Int32 Public...
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: public struct NETRESOURCE { public Int32...
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) { secondsToWait = Request.QueryString; if...
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 Sep 2005"); Console.WriteLine(dt1.Year);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.