Export (0) Print
Expand All

Convert.ChangeType Method

Returns an Object with a specified type and whose value is equivalent to a specified object.

Overload List

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

[Visual Basic] Overloads Public Shared Function ChangeType(Object, Type) As Object
[C#] public static object ChangeType(object, Type);
[C++] public: static Object* ChangeType(Object*, Type*);
[JScript] public static function ChangeType(Object, Type) : Object;

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

[Visual Basic] Overloads Public Shared Function ChangeType(Object, TypeCode) As Object
[C#] public static object ChangeType(object, TypeCode);
[C++] public: static Object* ChangeType(Object*, TypeCode);
[JScript] public static function ChangeType(Object, TypeCode) : Object;

Returns an Object with the specified Type and whose value is equivalent to the specified object. A parameter supplies culture-specific formatting information.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ChangeType(Object, Type, IFormatProvider) As Object
[C#] public static object ChangeType(object, Type, IFormatProvider);
[C++] public: static Object* ChangeType(Object*, Type*, IFormatProvider*);
[JScript] public static function ChangeType(Object, Type, IFormatProvider) : Object;

Returns an Object with the specified TypeCode and whose value is equivalent to the specified object. A parameter supplies culture-specific formatting information.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ChangeType(Object, TypeCode, IFormatProvider) As Object
[C#] public static object ChangeType(object, TypeCode, IFormatProvider);
[C++] public: static Object* ChangeType(Object*, TypeCode, IFormatProvider*);
[JScript] public static function ChangeType(Object, TypeCode, IFormatProvider) : Object;

Example

The following code sample illustrates the use of ChangeType:

[Visual Basic] 
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

[C#] 
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);        
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;

void main()
{

   Double d = -2.345;
   int i = *__try_cast<Int32*>(Convert::ChangeType(__box(d), __typeof(int)));

   Console::WriteLine(S"The double value {0} when converted to an int becomes {1}", __box(d), __box(i));

   String* s = S"12/12/98";
   DateTime dt = *__try_cast<DateTime*>(Convert::ChangeType(s, __typeof(DateTime)));

   Console::WriteLine(S"The String* value {0} when converted to a Date becomes {1}", s, __box(dt));        
}

[JScript] 
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);        
    }
}

See Also

Convert Class | Convert Members | System Namespace

Show:
© 2014 Microsoft