Re: Convert a string which might be a number to an int



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" <johansson.andersson@xxxxxxxxx> wrote in message news:%234MlkJS1IHA.5728@xxxxxxxxxxxxxxxxxxxxxxx
Hello!

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


"Bob Powell [MVP]" <bob@xxxxxxxxxxxxxxxxxxxxxxx> skrev i meddelandet
news:DFBED3B0-0F14-42F3-ABDF-EC53D8D6E8E5@xxxxxxxxxxxxxxxx
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" <johansson.andersson@xxxxxxxxx> wrote in message
news:O7ZpWDS1IHA.3860@xxxxxxxxxxxxxxxxxxxxxxx
> 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
>
>




.



Relevant Pages

  • Re: GDI+ throw OutOfMemoryException when draw a smill dash line.
    ... Find great Windows Forms articles in Windows Forms Tips and Tricks ... Answer those GDI+ questions with the GDI+ FAQ ... Otherwise, the exception is not throw, but the drawing is error. ... I think it is bug for GDI+ or .NET FWK. ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: ArgumentException @ Image.FromStream(stream)
    ... Find great Windows Forms articles in Windows Forms Tips and Tricks ... Answer those GDI+ questions with the GDI+ FAQ ... it is too ridicilious to get such an exception, ... > the related source code is shown below: ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Why "GDI+ Window" visible?
    ... It's difficult for us to locate the codes that raise the GDI+ Window, ... If it were an exception in the program, ... > Find great Windows Forms articles in Windows Forms Tips and Tricks ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Can a indexed pixel format be converted to some other format?
    ... > You don't need to get a Graphics object for an image that you want to save ... > Find great Windows Forms articles in Windows Forms Tips and Tricks ... > Answer those GDI+ questions with the GDI+ FAQ ... >> Anyway the following is the statement that causes the exception ...
    (microsoft.public.dotnet.languages.vb)
  • Re: INVALID_HANDLE_VALUE vs. NULL
    ... 0 (zero) can never be a valid handle value. ... Of course, ReadFilewill fail with this handle, ... > CloseHandle() does indeed fail when given a bad handle. ... > case it raises an exception. ...
    (microsoft.public.win32.programmer.kernel)

Loading