PC Review


Reply
Thread Tools Rate Thread

Convert a string which might be a number to an int

 
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      23rd Jun 2008
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>


 
Reply With Quote
 
 
 
 
Tony
Guest
Posts: n/a
 
      23rd Jun 2008
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


 
Reply With Quote
 
Micha³ Piaskowski
Guest
Posts: n/a
 
      23rd Jun 2008
one difference I know of is:

Convert.ToInt32(null) // returns 0
Int32.Parse(null) // throws exceprion.
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      23rd Jun 2008
Tony <(E-Mail Removed)> wrote:
> 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 - <(E-Mail Removed)>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
 
Reply With Quote
 
Tony
Guest
Posts: n/a
 
      23rd Jun 2008
Hello!

Yes TryParse seems to be better but just for curiosity does anyone have an
answer to my question.
//Tony


"Bob Powell [MVP]" <(E-Mail Removed)> skrev i meddelandet
newsFBED3B0-0F14-42F3-ABDF-(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >

>



 
Reply With Quote
 
qglyirnyfgfo@mailinator.com
Guest
Posts: n/a
 
      23rd Jun 2008
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.com> wrote:
> 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


 
Reply With Quote
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      23rd Jun 2008
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hello!
>
> Yes TryParse seems to be better but just for curiosity does anyone have an
> answer to my question.
> //Tony
>
>
> "Bob Powell [MVP]" <(E-Mail Removed)> skrev i meddelandet
> newsFBED3B0-0F14-42F3-ABDF-(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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
>> >
>> >

>>

>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert string to a number Rick Microsoft C# .NET 3 1st Dec 2008 10:52 PM
Convert a number from a string into number format =?Utf-8?B?U2FuZHk=?= Microsoft Access Queries 2 21st Feb 2007 08:23 PM
Convert a number to a name string =?Utf-8?B?TnVtYmVycyB0byBuYW1lIHN0cmluZw==?= Microsoft Excel Worksheet Functions 2 23rd Jun 2006 04:52 PM
Convert a String to an number 11Oppidan Microsoft Excel Programming 4 2nd Feb 2005 03:27 AM
convert string to number =?Utf-8?B?Q2hyaXN0aW5h?= Microsoft Excel Worksheet Functions 3 16th Nov 2004 02:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:17 AM.