String to Int

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

Kind of a newbie question, but I need to be able to convert a string to
an integer in C#, similar to VB's Val(). Anybody know how to do this?
I've tried C-style casting (obviously to no avail).
 
int.Parse("42");

You have to think about "what would provide the functionality?" to know where to look as there are no global functions. So what would know what an int is meant to look like as a string? An int! :-)

Regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

Kind of a newbie question, but I need to be able to convert a string to
an integer in C#, similar to VB's Val(). Anybody know how to do this?
I've tried C-style casting (obviously to no avail).

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004



[microsoft.public.dotnet.languages.csharp]
 
Dan said:
Kind of a newbie question, but I need to be able to convert a string to
an integer in C#, similar to VB's Val(). Anybody know how to do this?
I've tried C-style casting (obviously to no avail).

Use int.Parse(string) or Convert.ToInt32(string). Various overloads
allow you some different options.
 
If you would like to determine whether the string can be parsed into an int,
take a look at the double.TryParse() method. Otherwise, if your string is
input by the user, you'll want to surround it with a try/catch block.
Tom Clement
Apptero, Inc.
 
Back
Top