Convert::ChangeType Method (Object^, Type^)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

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

public:
static Object^ ChangeType(
	Object^ value,
	Type^ conversionType
)

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 null and conversionType is not a value type.

Exception Condition
InvalidCastException

This conversion is not supported.

-or-

value is null 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 null.

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.

No code example is currently available or this language may not be supported.

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<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.

No code example is currently available or this language may not be supported.

The following example illustrates the use of the ChangeType method.

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 );
}

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: