471,342 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Convert a string which might be a number to an int

Hello!

It seems to me that both Int32.Parse(..) and Convert.ToInt32(...) static
methods works in exactly the same way.
Both can throw an exeption.

So is it any different at all between these two ?

string input1 = Console.ReadLine();
string input2 = Console.ReadLine();

try
{
int number1 = Convert.ToInt32(input1);
int number2 = int32.Parse(input2);
}
catch
{...}

//Tony
Jun 27 '08 #1
6 1501
Use TryParse

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"Tony" <jo*****************@telia.comwrote in message
news:O7**************@TK2MSFTNGP05.phx.gbl...
Hello!

It seems to me that both Int32.Parse(..) and Convert.ToInt32(...) static
methods works in exactly the same way.
Both can throw an exeption.

So is it any different at all between these two ?

string input1 = Console.ReadLine();
string input2 = Console.ReadLine();

try
{
int number1 = Convert.ToInt32(input1);
int number2 = int32.Parse(input2);
}
catch
{...}

//Tony

Jun 27 '08 #2
one difference I know of is:

Convert.ToInt32(null) // returns 0
Int32.Parse(null) // throws exceprion.
Jun 27 '08 #3
Hello!

Yes TryParse seems to be better but just for curiosity does anyone have an
answer to my question.
//Tony
"Bob Powell [MVP]" <bo*@spamkillerbobpowell.netskrev i meddelandet
news:DF**********************************@microsof t.com...
Use TryParse

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"Tony" <jo*****************@telia.comwrote in message
news:O7**************@TK2MSFTNGP05.phx.gbl...
Hello!

It seems to me that both Int32.Parse(..) and Convert.ToInt32(...) static
methods works in exactly the same way.
Both can throw an exeption.

So is it any different at all between these two ?

string input1 = Console.ReadLine();
string input2 = Console.ReadLine();

try
{
int number1 = Convert.ToInt32(input1);
int number2 = int32.Parse(input2);
}
catch
{...}

//Tony

Jun 27 '08 #4
Tony <jo*****************@telia.comwrote:
It seems to me that both Int32.Parse(..) and Convert.ToInt32(...) static
methods works in exactly the same way.
Not quite. Convert.ToInt32((string)null) will return 0. int.Parse(null)
will throw an exception.
Both can throw an exeption.

So is it any different at all between these two ?

string input1 = Console.ReadLine();
string input2 = Console.ReadLine();

try
{
int number1 = Convert.ToInt32(input1);
int number2 = int32.Parse(input2);
}
catch
{...}
See above (beyond the fact that you mean int.Parse or Int32.Parse) -
but in this case the better solution would be to use int.TryParse.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #5
The internal implementation of Convert.ToInt32() is shown below:

public static int ToInt32(string value)
{
if (value == null)
{
return 0;
}
return int.Parse(value, CultureInfo.CurrentCulture);
}


On Jun 23, 6:11*am, "Tony" <johansson.anders...@telia.comwrote:
Hello!

It seems to me that both Int32.Parse(..) and Convert.ToInt32(...) static
methods works in exactly the same way.
Both can throw an exeption.

So is it any different at all between these two ?

string input1 = Console.ReadLine();
string input2 = Console.ReadLine();

try
{
* * int number1 = Convert.ToInt32(input1);
* * int number2 = int32.Parse(input2);}

catch
{...}

//Tony
Jun 27 '08 #6
Ok, well, the difference is that one returns zero (ConvertTo) and the other
throws an exception.
The problem is that if you really want to know if the string was in the
correct format then you need to catch an exception which to be honest is a
bad technique. Exceptions should be, well, the exception, rather than the
rule.
ConvertTo returns zero in the case of an error which is bad because every
mathemetician will scream that zero is a real number and is valid on it's
own.

Therefore, for best practices, TryParse satisfies the criteria of decoding
the value as well as discovering whether the string was a perfectly valid
zero or just some gibberish.
--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"Tony" <jo*****************@telia.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
Hello!

Yes TryParse seems to be better but just for curiosity does anyone have an
answer to my question.
//Tony
"Bob Powell [MVP]" <bo*@spamkillerbobpowell.netskrev i meddelandet
news:DF**********************************@microsof t.com...
>Use TryParse

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"Tony" <jo*****************@telia.comwrote in message
news:O7**************@TK2MSFTNGP05.phx.gbl...
Hello!

It seems to me that both Int32.Parse(..) and Convert.ToInt32(...)
static
methods works in exactly the same way.
Both can throw an exeption.

So is it any different at all between these two ?

string input1 = Console.ReadLine();
string input2 = Console.ReadLine();

try
{
int number1 = Convert.ToInt32(input1);
int number2 = int32.Parse(input2);
}
catch
{...}

//Tony


Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Edwin Quijada | last post: by
20 posts views Thread by Niyazi | last post: by
11 posts views Thread by Sudzzz | last post: by
8 posts views Thread by te509 | last post: by
reply views Thread by rosydwin | last post: by

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.