指定した TypeCode で、指定したオブジェクトに等しい値を持つ Object を返します。パラメータにより、カルチャに固有の書式情報が指定されます。
Overloads Public Shared Function ChangeType( _
ByVal value As Object, _ ByVal typeCode As TypeCode, _ ByVal provider As IFormatProvider _) As Object
[C#]
public static object ChangeType(objectvalue,TypeCodetypeCode,IFormatProviderprovider);
[C++]
public: static Object* ChangeType(Object* value,TypeCodetypeCode,IFormatProvider* provider);
[JScript]
public static function ChangeType(
value : Object,typeCode : TypeCode,provider : IFormatProvider) : Object;
パラメータ
- value
IConvertible インターフェイスを実装する Object 。 - typeCode
TypeCode 。 - provider
カルチャに固有の書式情報を提供する IFormatProvider インターフェイス実装。
戻り値
基になる TypeCode が typeCode であり、value と等価の値を持つオブジェクト。
または
value が null 参照 (Visual Basic では Nothing) であり、さらに typeCode が TypeCode.Empty の場合は null 参照 (Nothing) 。
例外
例外の種類 | 条件 |
---|---|
InvalidCastException | value は IConvertible を実装しません。 |
ArgumentException | typeCode が無効です。 |
解説
provider を使用すると、value の内容について、カルチャに固有の変換情報を指定できます。たとえば、 value が数値を表す String の場合、 provider によって、その数値の表記方法に関するカルチャに固有の情報を提供できます。
使用例
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 ファミリ, .NET Compact Framework - Windows CE .NET
参照
Convert クラス | Convert メンバ | System 名前空間 | Convert.ChangeType オーバーロードの一覧