Convert a string which might be a number to an int

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony

    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


  • Bob Powell [MVP]

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

    Use TryParse

    --
    --
    Bob Powell [MVP]
    Visual C#, System.Drawing

    Ramuseco Limited .NET consulting


    Find great Windows Forms articles in Windows Forms Tips and Tricks


    Answer those GDI+ questions with the GDI+ FAQ


    All new articles provide code in C# and VB.NET.
    Subscribe to the RSS feeds provided and never miss a new article.


    "Tony" <johansson.ande rsson@telia.com wrote in message
    news:O7ZpWDS1IH A.3860@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
    >
    >

    Comment

    • =?ISO-8859-2?Q?Micha=B3_Piaskowski?=

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

      one difference I know of is:

      Convert.ToInt32 (null) // returns 0
      Int32.Parse(nul l) // throws exceprion.

      Comment

      • Tony

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

        Hello!

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


        "Bob Powell [MVP]" <bob@spamkiller bobpowell.netsk rev i meddelandet
        news:DFBED3B0-0F14-42F3-ABDF-EC53D8D6E8E5@mi crosoft.com...
        Use TryParse
        >
        --
        --
        Bob Powell [MVP]
        Visual C#, System.Drawing
        >
        Ramuseco Limited .NET consulting

        >
        Find great Windows Forms articles in Windows Forms Tips and Tricks

        >
        Answer those GDI+ questions with the GDI+ FAQ

        >
        All new articles provide code in C# and VB.NET.
        Subscribe to the RSS feeds provided and never miss a new article.
        >
        >
        "Tony" <johansson.ande rsson@telia.com wrote in message
        news:O7ZpWDS1IH A.3860@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
        >

        Comment

        • Jon Skeet [C# MVP]

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

          Tony <johansson.ande rsson@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 - <skeet@pobox.co m>
          Web site: http://www.pobox.com/~skeet
          Blog: http://www.msmvps.com/jon_skeet
          C# in Depth: http://csharpindepth.com

          Comment

          • qglyirnyfgfo@mailinator.com

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

            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

            Comment

            • Bob Powell [MVP]

              #7
              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


              Find great Windows Forms articles in Windows Forms Tips and Tricks


              Answer those GDI+ questions with the GDI+ FAQ


              All new articles provide code in C# and VB.NET.
              Subscribe to the RSS feeds provided and never miss a new article.


              "Tony" <johansson.ande rsson@telia.com wrote in message
              news:%234MlkJS1 IHA.5728@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]" <bob@spamkiller bobpowell.netsk rev i meddelandet
              news:DFBED3B0-0F14-42F3-ABDF-EC53D8D6E8E5@mi crosoft.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" <johansson.ande rsson@telia.com wrote in message
              >news:O7ZpWDS1I HA.3860@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
              >
              >
              >>
              >
              >

              Comment

              Working...