473,670 Members | 2,401 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.ReadLin e();
string input2 = Console.ReadLin e();

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

//Tony
Jun 27 '08 #1
6 1582
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.com wrote in message
news:O7******** ******@TK2MSFTN GP05.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.ReadLin e();
string input2 = Console.ReadLin e();

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

//Tony

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

Convert.ToInt32 (null) // returns 0
Int32.Parse(nul l) // 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*@spamkiller bobpowell.netsk rev i meddelandet
news:DF******** *************** ***********@mic rosoft.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.com wrote in message
news:O7******** ******@TK2MSFTN GP05.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.ReadLin e();
string input2 = Console.ReadLin e();

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

//Tony

Jun 27 '08 #4
Tony <jo************ *****@telia.com 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.ReadLin e();
string input2 = Console.ReadLin e();

try
{
int number1 = Convert.ToInt32 (input1);
int number2 = int32.Parse(inp ut2);
}
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.co m>
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.Cur rentCulture);
}


On Jun 23, 6:11*am, "Tony" <johansson.ande rs...@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.ReadLin e();
string input2 = Console.ReadLin e();

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

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.com wrote in message
news:%2******** ********@TK2MSF TNGP06.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*@spamkiller bobpowell.netsk rev i meddelandet
news:DF******** *************** ***********@mic rosoft.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.com wrote in message
news:O7******* *******@TK2MSFT NGP05.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.ReadLin e();
string input2 = Console.ReadLin e();

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

//Tony


Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2622
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, entregas en todo el mundo, YupiMSN Compras: www.yupimsn.com/compras
4
9746
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 numeric value according to an arbitrary regular expression.
20
3431
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 ----> 0001 2 ----> 0002
52
1565
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? ----------------------------------------------------------------------- When formatting money for example, to format 6.57634 to 6.58, 6.5 to 6.50, and 6 to 6.00? Rounding of x.xx5 is uncertain, as such numbers are not represented exactly.
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? ----------------------------------------------------------------------- Javascript variables are loosely typed: the conversion between a string and a number happens automatically. Since plus (+) is also used as in string concatenation, « '1' + 1 » is equal to « '11' »: the String deciding...
19
3596
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 starting a new one] I'd still like to finish this rounding mess. As a startup lemma we can take that VK is the worst programmer of all times and places: let's move from here forward please. The usability of any program depends on exact behavior...
11
5044
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
3667
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 appreciate a prompt reply because I have a python assessment to submit. Thanks, Thomas
2
2210
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number? ----------------------------------------------------------------------- Javascript variables are loosely typed: the conversion between a string and a number happens automatically. Since plus (+) is also used as in string concatenation, ` '1' + 1 ` is equal to ` '11' `: the String deciding...
0
8471
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8388
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8907
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8817
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8593
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8663
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6218
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5687
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2804
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.