Returns an object of the specified type and whose value is equivalent to the specified object.
Namespace:
System
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Shared Function ChangeType ( _
value As Object, _
conversionType As Type _
) As Object
Dim value As Object
Dim conversionType As Type
Dim returnValue As Object
returnValue = Convert.ChangeType(value, _
conversionType)
public static Object ChangeType(
Object value,
Type conversionType
)
public:
static Object^ ChangeType(
Object^ value,
Type^ conversionType
)
public static function ChangeType(
value : Object,
conversionType : Type
) : Object
Return Value
Type:
System..::.ObjectAn object whose type is conversionType and whose value is equivalent to value.
-or-
A null reference (Nothing in Visual Basic), if value is nullNothingnullptra null reference (Nothing in Visual Basic) and conversionType is not a value type.
| Exception | Condition |
|---|
| InvalidCastException | This conversion is not supported. -or-
value is nullNothingnullptra null reference (Nothing in Visual Basic) 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 nullNothingnullptra null reference (Nothing in Visual Basic). |
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.
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
using System;
public class ChangeTypeTest {
public static void Main() {
Double d = -2.345;
int i = (int)Convert.ChangeType(d, typeof(int));
Console.WriteLine("The double value {0} when converted to an int becomes {1}", d, i);
string s = "12/12/98";
DateTime dt = (DateTime)Convert.ChangeType(s, typeof(DateTime));
Console.WriteLine("The string value {0} when converted to a Date becomes {1}", s, dt);
}
}
using namespace System;
int main()
{
Double d = -2.345;
int i = *safe_cast<Int32^>(Convert::ChangeType( d, int::typeid ));
Console::WriteLine( "The double value {0} when converted to an int becomes {1}", d, i );
String^ s = "12/12/98";
DateTime dt = *safe_cast<DateTime^>(Convert::ChangeType( s, DateTime::typeid ));
Console::WriteLine( "The string value {0} when converted to a Date becomes {1}", s, dt );
}
import System;
public class ChangeTypeTest {
public static function Main() {
var d : Double = -2.345;
var i : Int32 = Int32(Convert.ChangeType(d, Int32));
Console.WriteLine("The double value {0} when converted to an int becomes {1}", d, i);
var s : String = "12/12/98";
var dt : DateTime = DateTime(Convert.ChangeType(s, DateTime));
Console.WriteLine("The string value {0} when converted to a Date becomes {1}", s, dt);
}
}
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
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
Reference