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.

typenameDim 語句的 As 子句內合法的任何運算式,也就是任何資料類型、物件、結構、類別或介面的名稱。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:

  • 類型轉換函式,例如 CByteCDblCInt,這些函數會執行特定資料類型的轉換。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. Object 資料類型之間來回轉換時,它們可以提供比 CType 更好的效能。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.

如果沒有從 expression 定義轉換為 typename (例如,從 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. 如果未定義轉換,則會擲回中的 InvalidCastExceptionIf the conversion is undefined, an InvalidCastException in thrown. 例如,如果 expression 的類型為 Object,而其執行時間類型沒有轉換成 typename,就會發生這種情況。For example, this can happen if expression is of type Object and its run-time type has no conversion to typename.

如果 expressiontypename 的資料類型是您已定義的類別或結構,您可以將該類別或結構上的 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