473,216 Members | 1,490 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,216 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 1561
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Edwin Quijada | last post by:
There is a funciton to convert numbers into string?>? str:=string(3234); ?? _________________________________________________________________ Las mejores tiendas, los precios mas bajos,...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
20
by: Niyazi | last post by:
Hi all, I have a integer number from 1 to 37000. And I want to create a report in excel that shows in 4 alphanumeric length. Example: I can write the cutomerID from 1 to 9999 as: 1 ---->...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
11
by: Sudzzz | last post by:
Hi, I'm trying to convert a string something like this "{201,23,240,56,23,45,34,23}" into an array in C++ Please help. Thanks, Sudzzz
8
by: te509 | last post by:
Hi guys, does anybody know how to convert a long sequence of bits to a bit-string? I want to avoid this: '949456129574336313917039111707606368434510426593532217946399871489' I would...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.