Convert.ChangeType メソッド (Object, TypeCode, IFormatProvider)
指定した TypeCode で、指定したオブジェクトに等しい値を持つ Object を返します。パラメータにより、カルチャに固有の書式情報が指定されます。
名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文
Public Shared Function ChangeType ( _ value As Object, _ typeCode As TypeCode, _ provider As IFormatProvider _ ) As Object
Dim value As Object Dim typeCode As TypeCode Dim provider As IFormatProvider Dim returnValue As Object returnValue = Convert.ChangeType(value, typeCode, provider)
public static function ChangeType ( value : Object, typeCode : TypeCode, provider : IFormatProvider ) : Object
- typeCode
TypeCode。
戻り値
基になる TypeCode が typeCode であり、value と等価の値を持つオブジェクト。
または
value が null 参照 (Visual Basic では Nothing) に等しく、typeCode が Empty、String、または Object に等しい場合は null 参照 (Visual Basic では Nothing)。


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
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); } }
using namespace System; int main() { Double d = -2.345; int i = *safe_cast<Int32^>(Convert::ChangeType( d, int::typeid )); Console::WriteLine( "The double value {0} when converted to an int becomes {1}", d, i ); String^ s = "12/12/98"; DateTime dt = *safe_cast<DateTime^>(Convert::ChangeType( s, DateTime::typeid )); 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 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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Convert.ChangeType メソッド (Object, Type)
指定した Type で、指定したオブジェクトに等しい値を持つ Object を返します。
名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文
Dim value As Object Dim conversionType As Type Dim returnValue As Object returnValue = Convert.ChangeType(value, conversionType)
- conversionType
Type。
戻り値
Type が conversionType であり、value と等価の値を持つオブジェクト。
または
value が null 参照 (Visual Basic では Nothing) で、conversionType が値型ではない場合は、null 参照 (Visual Basic では Nothing)。



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
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); } }
using namespace System; int main() { Double d = -2.345; int i = *safe_cast<Int32^>(Convert::ChangeType( d, int::typeid )); Console::WriteLine( "The double value {0} when converted to an int becomes {1}", d, i ); String^ s = "12/12/98"; DateTime dt = *safe_cast<DateTime^>(Convert::ChangeType( s, DateTime::typeid )); 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 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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Convert.ChangeType メソッド (Object, Type, IFormatProvider)
指定した Type で、指定したオブジェクトに等しい値を持つ Object を返します。パラメータにより、カルチャに固有の書式情報が指定されます。
名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文
Public Shared Function ChangeType ( _ value As Object, _ conversionType As Type, _ provider As IFormatProvider _ ) As Object
Dim value As Object Dim conversionType As Type Dim provider As IFormatProvider Dim returnValue As Object returnValue = Convert.ChangeType(value, conversionType, provider)
public: static Object^ ChangeType ( Object^ value, Type^ conversionType, IFormatProvider^ provider )
public static function ChangeType ( value : Object, conversionType : Type, provider : IFormatProvider ) : Object
- conversionType
Type。
戻り値
Type が conversionType であり、value と等価の値を持つオブジェクト。
または
value の Type と conversionType が等しい場合は value。
または
value が null 参照 (Visual Basic では Nothing) で、conversionType が値型ではない場合は、null 参照 (Visual Basic では Nothing)。


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
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); } }
using namespace System; int main() { Double d = -2.345; int i = *safe_cast<Int32^>(Convert::ChangeType( d, int::typeid )); Console::WriteLine( "The double value {0} when converted to an int becomes {1}", d, i ); String^ s = "12/12/98"; DateTime dt = *safe_cast<DateTime^>(Convert::ChangeType( s, DateTime::typeid )); 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 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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Convert.ChangeType メソッド
指定したオブジェクトと等価の値と指定した型を持つ Object を返します。
オーバーロードの一覧
名前 | 説明 |
---|---|
Convert.ChangeType (Object, Type) | 指定した Type で、指定したオブジェクトに等しい値を持つ Object を返します。 |
Convert.ChangeType (Object, TypeCode) | 指定した TypeCode で、指定したオブジェクトに等しい値を持つ Object を返します。 |
Convert.ChangeType (Object, Type, IFormatProvider) | 指定した Type で、指定したオブジェクトに等しい値を持つ Object を返します。パラメータにより、カルチャに固有の書式情報が指定されます。 |
Convert.ChangeType (Object, TypeCode, IFormatProvider) | 指定した TypeCode で、指定したオブジェクトに等しい値を持つ Object を返します。パラメータにより、カルチャに固有の書式情報が指定されます。 |

Convert.ChangeType メソッド (Object, TypeCode)
指定した TypeCode で、指定したオブジェクトに等しい値を持つ Object を返します。
名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文
Dim value As Object Dim typeCode As TypeCode Dim returnValue As Object returnValue = Convert.ChangeType(value, typeCode)
- typeCode
TypeCode
戻り値
基になる TypeCode が typeCode であり、value と等価の値を持つオブジェクト。
または
value が null 参照 (Visual Basic では Nothing) に等しく、typeCode が Empty、String、または Object に等しい場合は null 参照 (Visual Basic では Nothing)。


ChangeType メソッドの使用方法を次のコード例に示します。この例では、可能であれば、渡された Object を TypeCode パラメータで指定した型に変更します。
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
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); } }
using namespace System; int main() { Double d = -2.345; int i = *safe_cast<Int32^>(Convert::ChangeType( d, int::typeid )); Console::WriteLine( "The double value {0} when converted to an int becomes {1}", d, i ); String^ s = "12/12/98"; DateTime dt = *safe_cast<DateTime^>(Convert::ChangeType( s, DateTime::typeid )); 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 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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- Convert.ChangeTypeのページへのリンク