CType 函式 (Visual Basic)CType Function (Visual Basic)

傳回運算式明確轉換至指定的資料類型、 物件、 結構、 類別或介面的結果。Returns the result of explicitly converting an expression to a specified data type, object, structure, class, or interface.

語法Syntax

CType(expression, typename)

組件Parts

expression 任何有效運算式。expression Any valid expression. 如果值expression超出所允許的範圍typename,Visual Basic 會擲回的例外狀況。If the value of expression is outside the range allowed by typename, Visual Basic throws an exception.

typename 任何運算式中合法As子句中的Dim陳述式,也就是任何資料類型、 物件、 結構、 類別或介面的名稱。typename Any expression that is legal within an As clause in a Dim statement, that is, the name of any data type, object, structure, class, or interface.

備註Remarks

提示

您也可以使用下列函式來執行類型轉換:You can also use the following functions to perform a type conversion:

  • 這類類型轉換函式CByteCDbl,和CInt執行特定的資料型別轉換。Type conversion functions such as CByte, CDbl, and CInt that perform a conversion to a specific data type. 如需詳細資訊,請參閱 < 類型轉換函式For more information, see Type Conversion Functions.
  • DirectCast 運算子或是TryCast 運算子DirectCast Operator or TryCast Operator. 這些運算子需要一種型別繼承自或實作另一個型別。These operators require that one type inherit from or implement the other type. 它們可以提供略微好的效能比CType來回轉換時Object資料型別。They can provide somewhat better performance than CType when converting to and from the Object data type.

CType 已編譯的內嵌,這表示,轉換程式碼就會評估運算式的程式碼的一部分。CType is compiled inline, which means that the conversion code is part of the code that evaluates the expression. 在某些情況下,更快的程式碼執行因為程序不會呼叫來執行轉換。In some cases, the code runs faster because no procedures are called to perform the conversion.

如果沒有任何轉換從定義expressiontypename(例如,從IntegerDate),Visual Basic 會顯示編譯時間錯誤訊息。If no conversion is defined from expression to typename (for example, from Integer to Date), Visual Basic displays a compile-time error message.

如果在執行階段發生轉換失敗,則會擲回適當的例外狀況。If a conversion fails at run time, the appropriate exception is thrown. 如果縮小轉換失敗,OverflowException是最常見的結果。If a narrowing conversion fails, an OverflowException is the most common result. 如果轉換,則未定義,InvalidCastException中擲回。If the conversion is undefined, an InvalidCastException in thrown. 比方說,如果此情形expression屬於型別Object不會轉換為其執行階段型別且typenameFor example, this can happen if expression is of type Object and its run-time type has no conversion to typename.

資料類型,是否expression或是typename是類別或結構定義之後,您可以定義CType上該類別或結構為轉換運算子。If the data type of expression or typename is a class or structure you've defined, you can define CType on that class or structure as a conversion operator. 這可讓CType充當多載運算子This makes CType act as an overloaded operator. 如果您這麼做,您可以控制轉換從您的類別或結構,包括可能擲回的例外狀況的行為。If you do this, you can control the behavior of conversions to and from your class or structure, including the exceptions that can be thrown.

多載化Overloading

CType可以也在類別或結構的程式碼外部定義上多載運算子。The CType operator can also be overloaded on a class or structure defined outside your code. 如果您的程式碼將轉換至或從這類類別或結構時,務必了解的行為及其CType運算子。If your code converts to or from such a class or structure, be sure you understand the behavior of its CType operator. 如需詳細資訊,請參閱 Operator ProceduresFor more information, see Operator Procedures.

轉換的動態物件Converting Dynamic Objects

動態物件的類型轉換由使用者定義的使用動態轉換TryConvertBindConvert方法。Type conversions of dynamic objects are performed by user-defined dynamic conversions that use the TryConvert or BindConvert methods. 如果您正在使用動態物件,使用CTypeDynamic方法轉換動態物件。If you're working with dynamic objects, use the CTypeDynamic method to convert the dynamic object.

範例Example

下列範例會使用CType函式將轉換運算式,以Single資料型別。The following example uses the CType function to convert an expression to the Single data type.

Dim testNumber As Long = 1000
' The following line of code sets testNewType to 1000.0.
Dim testNewType As Single = CType(testNumber, Single)

如需其他範例,請參閱 < 隱含和明確轉換For additional examples, see Implicit and Explicit Conversions.

另請參閱See also