隱含和明確轉換 (Visual Basic)Implicit and Explicit Conversions (Visual Basic)

隱含轉換在原始程式碼中不需要任何特殊語法。An implicit conversion does not require any special syntax in the source code. 在下列範例中, Visual Basic 會先將的值k隱含轉換為單精確度浮點數, 再將它指派給qIn the following example, Visual Basic implicitly converts the value of k to a single-precision floating-point value before assigning it to q.

Dim k As Integer
Dim q As Double
' Integer widens to Double, so you can do this with Option Strict On.
k = 432
q = k

明確轉換會使用類型轉換關鍵字。An explicit conversion uses a type conversion keyword. Visual Basic 提供了數個這類關鍵字, 可將括弧中的運算式強制轉型為所需的資料類型。Visual Basic provides several such keywords, which coerce an expression in parentheses to the desired data type. 這些關鍵字的作用就像函式, 但編譯器會產生內嵌程式碼, 因此執行的速度會比函式呼叫稍微快。These keywords act like functions, but the compiler generates the code inline, so execution is slightly faster than with a function call.

在上述範例的下列延伸模組中, CInt關鍵字會將的q值轉換回整數, 再將它指派給kIn the following extension of the preceding example, the CInt keyword converts the value of q back to an integer before assigning it to k.

' q had been assigned the value 432 from k.
q = Math.Sqrt(q)
k = CInt(q)
' k now has the value 21 (rounded square root of 432).

轉換關鍵字Conversion Keywords

下表顯示可用的轉換關鍵字。The following table shows the available conversion keywords.

類型轉換關鍵字Type conversion keyword 將運算式轉換為資料類型Converts an expression to data type 允許轉換之運算式的資料類型Allowable data types of expression to be converted
CBool Boolean 資料類型Boolean Data Type 任何數數值型別 (包括ByteSByte和列舉類型)、 StringObjectAny numeric type (including Byte, SByte, and enumerated types), String, Object
CByte Byte 資料類型Byte Data Type 任何數數值型別 (包括SByte和列舉類型)、 BooleanStringObjectAny numeric type (including SByte and enumerated types), Boolean, String, Object
CChar Char 資料類型Char Data Type StringObjectString, Object
CDate Date 資料類型Date Data Type StringObjectString, Object
CDbl Double 資料類型Double Data Type 任何數數值型別 (包括ByteSByte和列舉類型)、 BooleanStringObjectAny numeric type (including Byte, SByte, and enumerated types), Boolean, String, Object
CDec Decimal 資料類型Decimal Data Type 任何數數值型別 (包括ByteSByte和列舉類型)、 BooleanStringObjectAny numeric type (including Byte, SByte, and enumerated types), Boolean, String, Object
CInt Integer 資料類型Integer Data Type 任何數數值型別 (包括ByteSByte和列舉類型)、 BooleanStringObjectAny numeric type (including Byte, SByte, and enumerated types), Boolean, String, Object
CLng Long 資料類型Long Data Type 任何數數值型別 (包括ByteSByte和列舉類型)、 BooleanStringObjectAny numeric type (including Byte, SByte, and enumerated types), Boolean, String, Object
CObj Object 資料類型Object Data Type 任何型別Any type
CSByte SByte 資料類型SByte Data Type 任何數數值型別 (包括Byte和列舉類型)、 BooleanStringObjectAny numeric type (including Byte and enumerated types), Boolean, String, Object
CShort Short 資料類型Short Data Type 任何數數值型別 (包括ByteSByte和列舉類型)、 BooleanStringObjectAny numeric type (including Byte, SByte, and enumerated types), Boolean, String, Object
CSng Single 資料類型Single Data Type 任何數數值型別 (包括ByteSByte和列舉類型)、 BooleanStringObjectAny numeric type (including Byte, SByte, and enumerated types), Boolean, String, Object
CStr String 資料類型String Data Type 任何數數值型別 (包括ByteSByte和列舉類型)、 BooleanCharChar array、 DateObjectAny numeric type (including Byte, SByte, and enumerated types), Boolean, Char, Char array, Date, Object
CType 在逗號 (,) 後面指定的類型Type specified following the comma (,) 轉換成基本資料類型(包括基本類型的陣列) 時, 與對應的轉換關鍵字所允許的類型相同When converting to an elementary data type (including an array of an elementary type), the same types as allowed for the corresponding conversion keyword

轉換成複合資料型別時, 它所執行的介面和它所繼承的類別When converting to a composite data type, the interfaces it implements and the classes from which it inherits

轉換成您已CType多載的類別或結構時, 該類別或結構When converting to a class or structure on which you have overloaded CType, that class or structure
CUInt UInteger 資料類型UInteger Data Type 任何數數值型別 (包括ByteSByte和列舉類型)、 BooleanStringObjectAny numeric type (including Byte, SByte, and enumerated types), Boolean, String, Object
CULng ULong 資料類型ULong Data Type 任何數數值型別 (包括ByteSByte和列舉類型)、 BooleanStringObjectAny numeric type (including Byte, SByte, and enumerated types), Boolean, String, Object
CUShort UShort 資料類型UShort Data Type 任何數數值型別 (包括ByteSByte和列舉類型)、 BooleanStringObjectAny numeric type (including Byte, SByte, and enumerated types), Boolean, String, Object

CType 函式The CType Function

CType 函數會在兩個引數上運作。The CType Function operates on two arguments. 第一個是要轉換的運算式, 第二個是目的地資料類型或物件類別。The first is the expression to be converted, and the second is the destination data type or object class. 請注意, 第一個引數必須是運算式, 而不是類型。Note that the first argument must be an expression, not a type.

CType內嵌函式, 這表示已編譯的程式碼會進行轉換, 通常不會產生函式呼叫。CType is an inline function, meaning the compiled code makes the conversion, often without generating a function call. 這會改善效能。This improves performance.

如需CType與其他類型轉換關鍵字的比較, 請參閱DirectCast 運算子TryCast 運算子For a comparison of CType with the other type conversion keywords, see DirectCast Operator and TryCast Operator.

基本類型Elementary Types

下列範例示範 CType 的用法。The following example demonstrates the use of CType.

k = CType(q, Integer)
' The following statement coerces w to the specific object class Label.
f = CType(w, Label)

複合類型Composite Types

您可以使用CType將值轉換成複合資料型別以及基本類型。You can use CType to convert values to composite data types as well as to elementary types. 您也可以使用它將物件類別強制轉型為其中一個介面的類型, 如下列範例所示。You can also use it to coerce an object class to the type of one of its interfaces, as in the following example.

' Assume class cZone implements interface iZone.
Dim h As Object
' The first argument to CType must be an expression, not a type.
Dim cZ As cZone
' The following statement coerces a cZone object to its interface iZone.
h = CType(cZ, iZone)

陣列類型Array Types

CType也可以轉換陣列資料類型, 如下列範例所示。CType can also convert array data types, as in the following example.

Dim v() As classV
Dim obArray() As Object
' Assume some object array has been assigned to obArray.
' Check for run-time type compatibility.
If TypeOf obArray Is classV()
    ' obArray can be converted to classV.
    v = CType(obArray, classV())
End If

如需詳細資訊和範例, 請參閱陣列轉換For more information and an example, see Array Conversions.

定義 CType 的類型Types Defining CType

您可以在CType已定義的類別或結構上定義。You can define CType on a class or structure you have defined. 這可讓您將值轉換為類別或結構的類型。This allows you to convert values to and from the type of your class or structure. 如需詳細資訊和範例, 請參閱如何:定義轉換運算子For more information and an example, see How to: Define a Conversion Operator.

注意

搭配轉換關鍵字使用的值, 對於目的地資料類型而言必須是有效的, 否則會發生錯誤。Values used with a conversion keyword must be valid for the destination data type, or an error occurs. 例如, 如果Long您嘗試將轉換IntegerLong , 則的值Integer必須在資料類型的有效範圍內。For example, if you attempt to convert a Long to an Integer, the value of the Long must be within the valid range for the Integer data type.

警告

如果CType來源類型不是衍生自目的地類型, 指定要從一個類別類型轉換成另一個類別類型時, 會在執行時間失敗。Specifying CType to convert from one class type to another fails at run time if the source type does not derive from the destination type. 這類失敗InvalidCastException會擲回例外狀況。Such a failure throws an InvalidCastException exception.

不過, 如果其中一種類型是您已定義的結構或類別, 而且如果您已在CType該結構或類別上定義, 則轉換可能會在滿足的需求CType時成功。However, if one of the types is a structure or class you have defined, and if you have defined CType on that structure or class, a conversion can succeed if it satisfies the requirements of your CType. 請參閱How to:定義轉換運算子See How to: Define a Conversion Operator.

執行明確轉換也稱為將運算式轉換成指定的資料類型或物件類別。Performing an explicit conversion is also known as casting an expression to a given data type or object class.

另請參閱See also