共用方式為


使用 System.Convert 轉換資料型別

更新:2007 年 11 月

System.Convert 類別提供一組完整的方法,供支援的轉換使用。它提供了一種與語言無關的方式來執行轉換,所有以 Common Language Runtime 為目標的語言都可以使用。雖然不同的語言可能有不同的資料型別轉換技巧,但是 Convert 類別可確保所有常見的轉換都可用於泛用格式。這個類別會執行擴展轉換、縮小轉換,以及不相關資料型別的轉換。例如,從 String 型別到數字型別 (Numeric Type)、從 DateTime 型別到 String 型別,以及從 String 型別到 Boolean 型別的轉換都支援。如需可用轉換的清單,請參閱 Convert 類別中的方法清單Convert 類別會執行已檢查的轉換,如果轉換不受支援,就會擲回例外狀況。例外狀況經常是 OverflowException。如需支援轉換的清單,請參閱型別轉換表

您可以將要轉換的值傳遞至 Convert 類別的其中一種適合方法,並將傳回的值初始化成為新的變數。例如,下列程式碼會使用 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 類別的縮小轉換會變更轉換項目的值。下列程式碼範例會將 Double 轉換成 Int32 值。在這種情況下,會將 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.

請參閱

概念

明確轉換

其他資源

轉換型別