Share via


System.Convert を使用したデータ型の変換

更新 : 2007 年 11 月

System.Convert クラスには、サポートされている変換を行うためのメソッドがすべて用意されています。このクラスは、言語に中立的な形で変換を実行し、共通言語ランタイムに対応するすべての言語で使用できます。データ型を変換する手法は言語によって異なる場合がありますが、Convert クラスを使用すると、共通の変換をすべて汎用的な形で実行できます。このクラスは、拡大変換、縮小変換、および関連のないデータ型への変換を実行します。たとえば、String 型から数値型への変換、DateTime 型から String 型への変換、String 型から Boolean 型への各変換がサポートされています。実行できる変換の一覧については、Convert クラスのメソッドの一覧を参照してください。Convert クラスは、変換時にチェックを実行し、その変換がサポートされていない場合は必ず例外をスローします。多くの場合、この例外は OverflowException です。サポートされている変換の一覧については、型変換の表を参照してください。

変換対象の値を Convert クラスの適切なメソッドの 1 つに渡し、戻り値を新しい変数に初期化できます。Convert クラスを使用して String 値を Boolean 値に変換するコード例を次に示します。

Dim myString As String = "true"
Try
   Dim myBool As Boolean = Convert.ToBoolean(myString)
   Console.WriteLine(myBool)
Catch e As FormatException
   Console.WriteLine("{0} is not a Boolean value.", myString)
End Try   
' myBool has a value of True.
string myString = "true";
try
{
   bool myBool = Convert.ToBoolean(myString);
   Console.WriteLine(myBool);
}
catch (FormatException)
{
   Console.WriteLine("{0} is not a Boolean value.", myString);
}
// myBool has a value of True.

Convert クラスは、文字列を数値に変換する場合にも役立ちます。数値文字を含む文字列を Int32 値に変換するコード例を次に示します。

Dim newString As String = "123456789"
Try
   Dim myInt As Integer = Convert.ToInt32(newString)
   Console.WriteLine(myInt)
Catch e As FormatException
   Console.WriteLine("{0} does not represent a number.", newString)   
Catch e As OverflowException
   Console.WriteLine("{0} is out of range of the integer type.", _
                     newString)
End Try
' myInt has a value of 123456789.
string newString = "123456789";
try
{
   int myInt = Convert.ToInt32(newString);
   Console.WriteLine(myInt);
}
catch (FormatException)
{
   Console.WriteLine("{0} does not represent a number.", 
                     newString);   
}
catch (OverflowException)
{
   Console.WriteLine("{0} is out of range of the integer type.", 
                     newString);
}
// myInt has a value of 123456789.

Convert クラスは、特定の言語を使用している場合に、その言語では暗黙的に実行できない縮小変換を行うためにも使用できます。Convert.ToInt32 メソッドを使用して、Int64 からサイズがより小さい Int32 への縮小変換を実行するコード例を次に示します。

Dim myInt64 As Int64 = 123456789
Try
   Dim myInt As Integer = Convert.ToInt32(myInt64)
   Console.WriteLine(myInt)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0} to an integer.", _ 
                     myInt64)
End Try
' MyInt has a value of 123456789.
Int64 myInt64 = 123456789;
try
{
   int myInt = Convert.ToInt32(myInt64);
   Console.WriteLine(myInt);
}
catch (OverflowException)
{
   Console.WriteLine("Unable to convert {0} to a 32-bit integer.", 
                     myInt64);
}
// myInt has a value of 123456789.

Convert クラスを使用して縮小変換を実行すると、変換元の項目の値が変更される場合があります。DoubleInt32 値に変換するコード例を次に示します。この例では、変換を完了させるために、値 42.72 が 43 に丸められています。

Dim myDouble As Double = 42.72
Try
   Dim myInt As Integer = Convert.ToInt32(myDouble)
   Console.WriteLine(myInt)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0} to an integer.", myDouble)
End Try   
' MyInt has a value of 43.
Double myDouble = 42.72;
try 
{
   int myInt = Convert.ToInt32(myDouble);
   Console.WriteLine(myInt);
}
catch (OverflowException)
{
   Console.WriteLine("Unable to convert {0} to an integer.", myDouble);
}
// myInt has a value of 43.

参照

概念

明示的な変換

その他の技術情報

型の変換