Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
how to convert from string to integer in c# ?
Posted 13-Feb-13 12:31pm
desho215
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Very interesting thing in conversion is Input string, here some examples to understand convertion..
 
1.
int numVal = Int32.Parse("-105");
Console.WriteLine(numVal);
// Output: -105
2.
int j;
bool result = Int32.TryParse("-105", out j);
if (true == result)
    Console.WriteLine(j);
else
    Console.WriteLine("String could not be parsed.");
// Output: -105
3.
try
{
    int m = Int32.Parse("abc");
}
catch (FormatException e)
{
    Console.WriteLine(e.Message);
}
// Output: Input string was not in a correct format.
4.
string inputString = "abc";
int numValue;
bool parsed = Int32.TryParse(inputString, out numValue);
 
if (!parsed)
    Console.WriteLine("Int32.TryParse could not parse '{0}' to an int.\n", inputString);
// Output: Int32.TryParse could not parse 'abc' to an int.
 
From above example you come to know that input string should be in correct format..
 
You can refer below link..
http://msdn.microsoft.com/en-us/library/bb397679.aspx[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

use Int32.TryParse[^] or Inst32.Parse[^]
int number;
bool result = Int32.TryParse("string value to convert", out number);
//or
number = Int32.Parse("string value to convert");
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 13-Feb-13 18:13pm
   
5ed. Parse (which is also worth mentioning) and TryParse is much better than convert because it tells us what really happens. "Convert" is a really bad word literally haunting this forum.
—SA
Jibesh at 13-Feb-13 18:22pm
   
That is reasonable,updated my answer.
 
Thanks.
Sergey Alexandrovich Kryukov at 13-Feb-13 18:38pm
   
Nice. Thank you.
—SA
Marcus Kramer at 14-Feb-13 9:01am
   
+5. TryParse is the way to go.
Jibesh at 14-Feb-13 12:11pm
   
Thank You marcus
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

int number = Convert.ToInt32("string to convert");
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

public static bool TryParse(
    string s,
    out int result
)
  Permalink  
v2
Comments
Manfred R. Bihy at 18-Feb-13 8:43am
   
I'm not sure what value this solution adds considering the quality of solutions 2 & 4.
Care to elaborate what you are trying to accomplish here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 310
1 OriginalGriff 285
2 Afzaal Ahmad Zeeshan 200
3 Sergey Alexandrovich Kryukov 195
4 BillWoodruff 180
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100