Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
Convert Class
Convert Methods
ToInt32 Method
 ToInt32 Method (String)

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Convert..::.ToInt32 Method (String)

Updated: May 2009

Converts the specified string representation of a number to an equivalent 32-bit signed integer.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Shared Function ToInt32 ( _
    value As String _
) As Integer
Visual Basic (Usage)
Dim value As String
Dim returnValue As Integer

returnValue = Convert.ToInt32(value)
C#
public static int ToInt32(
    string value
)
Visual C++
public:
static int ToInt32(
    String^ value
)
JScript
public static function ToInt32(
    value : String
) : int

Parameters

value
Type: System..::.String
A string that contains the number to convert.

Return Value

Type: System..::.Int32
A 32-bit signed integer that is equivalent to the number in value, or 0 (zero) if value is nullNothingnullptra null reference (Nothing in Visual Basic).
ExceptionCondition
FormatException

value does not consist of an optional sign followed by a sequence of digits (0 through 9).

OverflowException

value represents a number that is less than Int32..::.MinValue or greater than Int32..::.MaxValue.

The result of invoking the Int32..::.Parse method on value.

If you prefer not to handle an exception if the conversion fails, you can call the Int32..::.TryParse method instead. It returns a Boolean value that indicates whether the conversion succeeded or failed.

The following example attempts to convert each element in a numeric string array to an integer.

Visual Basic
Dim values() As String = { "One", "1.34e28", "-26.87", "-18", "-6.00", _
                           " 0", "137", "1601.9", Int32.MaxValue.ToString() }
Dim result As Integer

For Each value As String In values
   Try
      result = Convert.ToInt32(value)
      Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", _
                        value.GetType().Name, value, result.GetType().Name, result)
   Catch e As OverflowException
      Console.WriteLine("{0} is outside the range of the Int32 type.", value)
   Catch e As FormatException
      Console.WriteLine("The {0} value '{1}' is not in a recognizable format.", _
                        value.GetType().Name, value)
   End Try   
Next                                 
' The example displays the following output:
'    The String value 'One' is not in a recognizable format.
'    The String value '1.34e28' is not in a recognizable format.
'    The String value '-26.87' is not in a recognizable format.
'    Converted the String value '-18' to the Int32 value -18.
'    The String value '-6.00' is not in a recognizable format.
'    Converted the String value ' 0' to the Int32 value 0.
'    Converted the String value '137' to the Int32 value 137.
'    The String value '1601.9' is not in a recognizable format.
'    Converted the String value '2147483647' to the Int32 value 2147483647.

C#
string[] values = { "One", "1.34e28", "-26.87", "-18", "-6.00",
                    " 0", "137", "1601.9", Int32.MaxValue.ToString() };
int result;

foreach (string value in values)
{
   try {
      result = Convert.ToInt32(value);
      Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.",
                        value.GetType().Name, value, result.GetType().Name, result);
   }
   catch (OverflowException) {
      Console.WriteLine("{0} is outside the range of the Int32 type.", value);
   }   
   catch (FormatException) {
      Console.WriteLine("The {0} value '{1}' is not in a recognizable format.",
                        value.GetType().Name, value);
   }   
}                                 
// The example displays the following output:
//    The String value 'One' is not in a recognizable format.
//    The String value '1.34e28' is not in a recognizable format.
//    The String value '-26.87' is not in a recognizable format.
//    Converted the String value '-18' to the Int32 value -18.
//    The String value '-6.00' is not in a recognizable format.
//    Converted the String value ' 0' to the Int32 value 0.
//    Converted the String value '137' to the Int32 value 137.
//    The String value '1601.9' is not in a recognizable format.
//    Converted the String value '2147483647' to the Int32 value 2147483647.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

May 2009

Expanded the Remarks section.

Customer feedback.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
System.FormatException can occur even with a valid numeric string parameter      gsohl   |   Edit   |   Show History
See KB article 919236 (http://support.microsoft.com/kb/919236). This article is about SQL Server Management Studio, which is a .NET 2.0 (I think) app. I had this error at a customer site with our .NET 3.5 app. This problem occurs if the value of the following registry key is not empty or is set to an invalid value: HKEY_CURRENT_USER\Control Panel\International\sPositiveSign. The article has instructions on resolving the issue.
Int32 Value Range      Cory ... Thomas Lee   |   Edit   |   Show History

The System.Int32 value type represents signed integers with values ranging from negative 2,147,483,648 through positive 2,147,483,647.

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker