Thread: difference between Convert & Int32

  1. #1
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    Question difference between Convert & Int32

    reading a value in with Console.ReadLine i noticed there are two different methods i can use from the FCL

    Code:
    n=Convert.ToInt32(Console.ReadLine());
    Code:
    n=Int32.Parse(Console.ReadLine());
    is there any difference in the value n ? (ie the range)

    or are they 2 methods that do the same thing?
    Last edited by luigi40; 12-01-2005 at 03:01 PM. Reason: change

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Quote Originally Posted by luigi40
    reading a value in with Console.ReadLine i noticed there are two different methods i can use from the FCL

    Code:
    n=Convert.ToInt32(Console.ReadLine());
    Code:
    n=Int32.Parse(Console.ReadLine());
    is there any difference in the value n ? (ie the range)

    or are they 2 methods that do the same thing?
    I believe they are different, as Int32.Parse can convert "1,000" to 1000, while I don't believe Convert.ToInt32 does. If it does, it would call Int32.Parse anyway, so it will be a little bit slower, so I'd call Int32.Parse myself.

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Convert.ToInt32(string) and Int32.Parse(string) are two different methods that you can use to accomplish the same thing. The Convert method is there for completeness (you can convert from just about every other data type too), and the Parse method has overloads that are useful if you need to specify an IFormatProvider (which you probably don't if you're not worrying about internationalization) or a NumberStyles enumeration. The NumberStyles enumeration is pretty handy; it lets you decide what extra characters are allowed inside the number. For instance, you might want to allow a currency symbol, but you might want to disallow exponents.

    In general, I'd use Int32.Parse. It allows more flexibility than Convert.ToInt32.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61
    thanks for replies people


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert double to string problem
    By gandalf_bar in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2004, 05:14 AM
  2. Difference between using atoi and scanf
    By James00 in forum C Programming
    Replies: 4
    Last Post: 04-12-2003, 09:59 AM
  3. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 12:53 PM
  4. Convert a string to upper case?
    By jpp1cd in forum C Programming
    Replies: 2
    Last Post: 12-12-2002, 07:49 PM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM