473,513 Members | 13,099 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1569
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
2610
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
9704
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
3398
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
1565
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
1
369
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
3567
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
5033
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
3648
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
2195
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
0
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5079
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.