Converting Data Types Using System.Convert

The System.Convert class provides a complete set of methods for supported conversions. It provides a language-neutral way to perform conversions and is available to all languages that target the common language runtime. Whereas different languages might have different techniques for converting data types, the Convert class ensures that all common conversions are available in a generic format. This class performs widening conversions, narrowing conversions, as well as conversions to unrelated data types. For example, conversions from String types to numeric types, DateTime types to String types, and String types to Boolean types are supported. For a list of available conversions, see the list of methods in the Convert class. The Convert class performs checked conversions and always throws an exception if the conversion is not supported. The exception is often an OverflowException. For a list of supported conversions, see the type conversion tables.

You can pass the value that you want to convert to one of the appropriate methods in the Convert class and initialize the returned value to a new variable. For example, the following code uses the Convert class to transform a String value into a Boolean value.

Dim myString As String = "true" 
Try 
   Dim myBool As Boolean = Convert.ToBoolean(myString)
   Console.WriteLine(myBool)
Catch e As FormatException
   Console.WriteLine("{0} is not a Boolean value.", myString)
End Try    
' myBool has a value of True.
string myString = "true";
try
{
   bool myBool = Convert.ToBoolean(myString);
   Console.WriteLine(myBool);
}
catch (FormatException)
{
   Console.WriteLine("{0} is not a Boolean value.", myString);
}
// myBool has a value of True.

The Convert class is also useful if you have a string that you want to convert to a numeric value. The following code example converts a string that contains numeric characters into an Int32 value.

Dim newString As String = "123456789" 
Try 
   Dim myInt As Integer = Convert.ToInt32(newString)
   Console.WriteLine(myInt)
Catch e As FormatException
   Console.WriteLine("{0} does not represent a number.", newString)   
Catch e As OverflowException
   Console.WriteLine("{0} is out of range of the integer type.", _
                     newString)
End Try 
' myInt has a value of 123456789.
string newString = "123456789";
try
{
   int myInt = Convert.ToInt32(newString);
   Console.WriteLine(myInt);
}
catch (FormatException)
{
   Console.WriteLine("{0} does not represent a number.", 
                     newString);   
}
catch (OverflowException)
{
   Console.WriteLine("{0} is out of range of the integer type.", 
                     newString);
}
// myInt has a value of 123456789.

The Convert class can also be used for a narrowing conversion that cannot be performed implicitly in the particular language you are using. The following code example shows a narrowing conversion from an Int64 to a smaller Int32 using the Convert.ToInt32 method.

Dim myInt64 As Int64 = 123456789
Try 
   Dim myInt As Integer = Convert.ToInt32(myInt64)
   Console.WriteLine(myInt)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0} to an integer.", _ 
                     myInt64)
End Try 
' MyInt has a value of 123456789.
Int64 myInt64 = 123456789;
try
{
   int myInt = Convert.ToInt32(myInt64);
   Console.WriteLine(myInt);
}
catch (OverflowException)
{
   Console.WriteLine("Unable to convert {0} to a 32-bit integer.", 
                     myInt64);
}
// myInt has a value of 123456789.

Sometimes performing a narrowing conversion with the Convert class changes the value of the item being converted. The following code example converts a Double into an Int32 value. In this case, the value is rounded from 42.72 to 43 in order to complete the conversion.

Dim myDouble As Double = 42.72
Try 
   Dim myInt As Integer = Convert.ToInt32(myDouble)
   Console.WriteLine(myInt)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0} to an integer.", myDouble)
End Try    
' MyInt has a value of 43.
Double myDouble = 42.72;
try 
{
   int myInt = Convert.ToInt32(myDouble);
   Console.WriteLine(myInt);
}
catch (OverflowException)
{
   Console.WriteLine("Unable to convert {0} to an integer.", myDouble);
}
// myInt has a value of 43.

See Also

Concepts

Explicit Conversion

Other Resources

Converting Types