指定した Type で、指定したオブジェクトに等しい値を持つ Object を返します。
Overloads Public Shared Function ChangeType( _
ByVal value As Object, _ ByVal conversionType As Type _) As Object
[C#]
public static object ChangeType(objectvalue,TypeconversionType);
[C++]
public: static Object* ChangeType(Object* value,Type* conversionType);
[JScript]
public static function ChangeType(
value : Object,conversionType : Type) : Object;
パラメータ
- value
IConvertible インターフェイスを実装する Object 。 - conversionType
Type 。
戻り値
Type が conversionType であり、value と等価の値を持つオブジェクト。
例外
例外の種類 | 条件 |
---|---|
InvalidCastException | value は IConvertible を実装しません。 |
解説
このメソッドは、現在のスレッドのカルチャを使用して変換を実行します。
使用例
ChangeType の使用方法については、次のコード例を参照してください。
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);
}
}
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
Convert クラス | Convert メンバ | System 名前空間 | Convert.ChangeType オーバーロードの一覧