Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Convert.ChangeType Method (Object, Type, IFormatProvider)

.NET Framework 1.1

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

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

Parameters

value
An Object that implements the IConvertible interface.
conversionType
A Type.
provider
An IFormatProvider interface implementation that supplies culture-specific formatting information.

Return Value

An object whose Type is conversionType and whose value is equivalent to value.

-or-

A null reference (Nothing in Visual Basic), if value and conversionType are null references (Nothing).

-or-

value, if the Type of value and conversionType are equal.

Exceptions

Exception Type Condition
InvalidCastException value does not implement IConvertible and conversionType is not a null reference (Nothing in Visual Basic) or equal to the Type of value.
ArgumentException conversionType is invalid.

Remarks

provider enables the user to specify culture-specific conversion information about the contents of value. For example, if value is a String that represents a number, provider could supply culture-specific information about the notation used to represent that number.

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

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Convert Class | Convert Members | System Namespace | Convert.ChangeType Overload List

Show:
© 2015 Microsoft