Convert.ChangeTypeとは? わかりやすく解説

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 Object ChangeType (
    Object value,
    TypeCode typeCode,
    IFormatProvider provider
)
public:
static Object^ ChangeType (
    Object^ value, 
    TypeCode typeCode, 
    IFormatProvider^ provider
)
public static Object ChangeType (
    Object value, 
    TypeCode typeCode, 
    IFormatProvider provider
)
public static function ChangeType
 (
    value : Object, 
    typeCode : TypeCode, 
    provider : IFormatProvider
) : Object

パラメータ

value

IConvertible インターフェイス実装する Object

typeCode

TypeCode。

provider

カルチャに固有の書式情報提供する IFormatProvider インターフェイス実装

戻り値
基になる TypeCodetypeCode であり、value等価の値を持つオブジェクト。 または valuenull 参照 (Visual Basic では Nothing) に等しくtypeCodeEmptyString、または Object等し場合null 参照 (Visual Basic では Nothing)。

例外例外
例外種類条件

InvalidCastException

この変換サポートされていません。

または

valuenull 参照 (Visual Basic では Nothing) であり、typeCode値型指定してます。

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
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);        
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Convert.ChangeType メソッド (Object, Type)

指定した Type で、指定したオブジェクト等しい値を持つ Object返します

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function ChangeType
 ( _
    value As Object, _
    conversionType As Type _
) As Object
Dim value As Object
Dim conversionType As Type
Dim returnValue As Object

returnValue = Convert.ChangeType(value, conversionType)
public static Object ChangeType (
    Object value,
    Type conversionType
)
public:
static Object^ ChangeType (
    Object^ value, 
    Type^ conversionType
)
public static Object ChangeType (
    Object value, 
    Type conversionType
)
public static function ChangeType
 (
    value : Object, 
    conversionType : Type
) : Object

パラメータ

value

IConvertible インターフェイス実装する Object

conversionType

Type

戻り値
TypeconversionType であり、value等価の値を持つオブジェクト。 または valuenull 参照 (Visual Basic では Nothing) で、conversionType値型ではない場合は、null 参照 (Visual Basic では Nothing)。

例外例外
例外種類条件

InvalidCastException

この変換サポートされていません。

または

valuenull 参照 (Visual Basic では Nothing) であり、conversionType値型です。

ArgumentNullException

conversionTypenull 参照 (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);        
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

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 Object^ ChangeType (
    Object^ value, 
    Type^ conversionType, 
    IFormatProvider^ provider
)
public static Object ChangeType (
    Object value, 
    Type conversionType, 
    IFormatProvider provider
)
public static function ChangeType
 (
    value : Object, 
    conversionType : Type, 
    provider : IFormatProvider
) : Object

パラメータ

value

IConvertible インターフェイス実装する Object

conversionType

Type

provider

カルチャに固有の書式情報提供する IFormatProvider インターフェイス実装

戻り値
TypeconversionType であり、value等価の値を持つオブジェクト。 または valueTypeconversionType等し場合value。 または valuenull 参照 (Visual Basic では Nothing) で、conversionType値型ではない場合は、null 参照 (Visual Basic では Nothing)。

例外例外
例外種類条件

InvalidCastException

この変換サポートされていません。

または

valuenull 参照 (Visual Basic では Nothing) であり、conversionType値型です。

ArgumentNullException

conversionTypenull 参照 (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);        
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Convert.ChangeType メソッド

指定したオブジェクト等価の値と指定した型を持つ Object返します
オーバーロードの一覧オーバーロードの一覧

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

.NET Compact Framework によってサポートされています。

Convert.ChangeType (Object, TypeCode, IFormatProvider) 指定した TypeCode で、指定したオブジェクト等しい値を持つ Object返しますパラメータにより、カルチャに固有の書式情報指定されます。

.NET Compact Framework によってサポートされています。

参照参照

Convert.ChangeType メソッド (Object, TypeCode)

指定した TypeCode で、指定したオブジェクト等しい値を持つ Object返します

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function ChangeType
 ( _
    value As Object, _
    typeCode As TypeCode _
) As Object
Dim value As Object
Dim typeCode As TypeCode
Dim returnValue As Object

returnValue = Convert.ChangeType(value, typeCode)
public static Object ChangeType (
    Object value,
    TypeCode typeCode
)
public:
static Object^ ChangeType (
    Object^ value, 
    TypeCode typeCode
)
public static Object ChangeType (
    Object value, 
    TypeCode typeCode
)
public static function ChangeType
 (
    value : Object, 
    typeCode : TypeCode
) : Object

パラメータ

value

IConvertible インターフェイス実装する Object

typeCode

TypeCode

戻り値
基になる TypeCodetypeCode であり、value等価の値を持つオブジェクト。 または valuenull 参照 (Visual Basic では Nothing) に等しくtypeCodeEmptyString、または Object等し場合null 参照 (Visual Basic では Nothing)。

例外例外
例外種類条件

InvalidCastException

この変換サポートされていません。

または

valuenull 参照 (Visual Basic では Nothing) であり、typeCode値型指定してます。

ArgumentException

typeCode無効です。

使用例使用例

ChangeType メソッド使用方法次のコード例示します。この例では、可能であれば渡されObjectTypeCode パラメータ指定した型に変更します

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);        
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Convert.ChangeType」の関連用語

Convert.ChangeTypeのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Convert.ChangeTypeのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS