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

## 語法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

• 這類類型轉換函式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.
• 這些運算子需要一種型別繼承自或實作另一個型別。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.

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.

## 範例Example

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