Export (0) Print
Expand All

Convert.ChangeType Method (Object, Type)

Returns an object of the specified type and whose value is equivalent to the specified object.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Shared Function ChangeType ( _
	value As Object, _
	conversionType As Type _
) As Object

Parameters

value
Type: System.Object

An object that implements the IConvertible interface.

conversionType
Type: System.Type

The type of object to return.

Return Value

Type: System.Object
An object whose type is conversionType and whose value is equivalent to value.
-or-
A null reference (Nothing in Visual Basic), if value is Nothing and conversionType is not a value type.

ExceptionCondition
InvalidCastException

This conversion is not supported.

-or-

value is Nothing and conversionType is a value type.

-or-

value does not implement the IConvertible interface.

FormatException

value is not in a format recognized by conversionType.

OverflowException

value represents a number that is out of the range of conversionType.

ArgumentNullException

conversionType is Nothing.

ChangeType is a general-purpose conversion method that converts the object specified by value to conversionType. The value parameter can be an object of any type, and conversionType can also be a Type object that represents any base or custom type. For the conversion to succeed, value must implement the IConvertible interface, because the method simply wraps a call to an appropriate IConvertible method. The method requires that conversion of value to conversionType be supported.

This method uses the current thread's culture for the conversion.

Notes to Callers

The ChangeType(Object, Type) method can convert an enumeration value to another type. However, it cannot convert another type to an enumeration value, even if the source type is the underlying type of the enumeration. To convert a type to an enumeration value, use a casting operator (in C#) or a conversion function (in Visual Basic). The following example illustrates the conversion to and from a Continent enumeration value.

Public Enum Continent As Integer
   Africa = 0
   Antarctica = 1
   Asia = 2
   Australia = 3
   Europe = 4
   NorthAmerica = 5
   SouthAmerica = 6
End Enum 

Module Example
   Public Sub Main()
      ' Convert a Continent to a Double. 
      Dim cont As Continent = Continent.NorthAmerica
      Console.WriteLine("{0:N2}", 
                        Convert.ChangeType(cont, GetType(Double)))

      ' Convert a Double to a Continent. 
      Dim number As Double = 6.0
      Try
         Console.WriteLine("{0}", 
                           Convert.ChangeType(number, GetType(Continent)))
      Catch e As InvalidCastException
         Console.WriteLine("Cannot convert a Double to a Continent")
      End Try

      Console.WriteLine("{0}", CType(number, Continent))   
   End Sub 
End Module 
' The example displays the following output: 
'    5.00 
'    Cannot convert a Double to a Continent 
'    SouthAmerica

The ChangeType(Object, Type) method can convert a nullable type to another type. However, it cannot convert another type to a value of a nullable type, even if conversionType is the underlying type of the Nullable(Of T). To perform the conversion, you can use a casting operator (in C#) or a conversion function (in Visual Basic). The following example illustrates the conversion to and from a nullable type.

Module Example
   Public Sub Main()
      Dim intValue1 As Integer? = 12893
      Dim dValue1 As Double = CType(Convert.ChangeType(intValue1, GetType(Double)), Double)
      Console.WriteLine("{0} ({1})--> {2} ({3})", intValue1, intValue1.GetType().Name,
                        dValue1, dValue1.GetType().Name)


      Dim fValue1 As Single = 16.3478
      Dim intValue2 As Integer? = CType(fValue1, Integer) 
      Console.WriteLine("{0} ({1})--> {2} ({3})", fValue1, fValue1.GetType().Name,
                        intValue2, intValue2.GetType().Name)
   End Sub 
End Module 
' The example displays the following output: 
'       12893 (Int32)--> 12893 (Double) 
'       16.3478 (Single)--> 16 (Int32)

The following example illustrates the use of the ChangeType method.

Imports System

Public Class ChangeTypeTest

    Public Shared Sub Main()
        Dim d As [Double] = - 2.345
        Dim i As Integer = CInt(Convert.ChangeType(d, GetType(Integer)))

        Console.WriteLine("The double value {0} when converted to an int becomes {1}", d, i)
        Dim s As String = "12/12/98" 
        Dim dt As DateTime = CType(Convert.ChangeType(s, GetType(DateTime)), DateTime)

        Console.WriteLine("The string value {0} when converted to a Date becomes {1}", s, dt)
    End Sub 'Main
End Class 'ChangeTypeTest

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft