Export (0) Print
Expand All

Convert.ChangeType Method (Object, TypeCode)

Returns an Object with the specified TypeCode and whose value is equivalent to the specified object.

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

public static Object ChangeType (
	Object value,
	TypeCode typeCode
)
public static Object ChangeType (
	Object value, 
	TypeCode typeCode
)
public static function ChangeType (
	value : Object, 
	typeCode : TypeCode
) : Object
Not applicable.

Parameters

value

An Object that implements the IConvertible interface.

typeCode

A TypeCode

Return Value

An object whose underlying TypeCode is typeCode and whose value is equivalent to value. -or- a null reference (Nothing in Visual Basic), if value equals a null reference (Nothing in Visual Basic) and typeCode equals Empty, String, or Object.

Exception typeCondition

InvalidCastException

This conversion is not supported.

-or-

value is a null reference (Nothing in Visual Basic) and typeCode specifies a value type.

ArgumentException

typeCode is invalid.

The following code sample illustrates the use of the ChangeType method, changing the passed Object to the type specified by the TypeCode parameter, if possible.

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

import System.*;

public class ChangeTypeTest
{
    public static void main(String[] args)
    {
        Double d = new Double(-2.345);
        int i = Convert.ToInt32((Convert.ChangeType(d, int.class.ToType())));

        Console.WriteLine("The double value {0} when converted to an "
            + "int becomes {1}", System.Convert.ToString(d),
            System.Convert.ToString(i));

        String s = "12/12/98";
        DateTime dt = (DateTime)(Convert.ChangeType(s, DateTime.
            class.ToType()));
        Console.WriteLine("The string value {0} when converted to "
            + "a Date becomes {1}", s, dt);
    } //main
} //ChangeTypeTest

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 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft