DirectCast 運算子 (Visual Basic)

依據繼承或實作引入型別轉換作業。

備註

DirectCast 不會將 Visual Basic 執行階段 Helper 常式用於轉換,因此在資料型別 Object 間進行轉換時,它可提供的效能比 CType 還好。

使用 DirectCast 關鍵字的方式與使用 CType 函式 (Visual Basic)TryCast 運算子 (Visual Basic) 關鍵字的方式相同。 您提供的第一個引數是運算式,第二個引數則是用來轉換它的型別。 DirectCast 需要這兩個引數之資料型別之間的繼承或實作關聯性。 這表示其中一個型別必須繼承自另一個型別或實作另一個型別。

錯誤和失敗

如果 DirectCast 偵測到沒有繼承或實作關聯性存在,則會產生編譯器錯誤。 但沒有編譯器錯誤並不保證轉換成功。 如果正在縮小所要的轉換,則在執行階段可能會失敗。 如果發生這種情況,執行階段會擲回 InvalidCastException 錯誤。

轉換關鍵字

型別轉換關鍵字的比較如下。

關鍵字

資料型別

引數關聯性

執行階段失敗

CType 函式 (Visual Basic)

任何資料型別

必須在這兩個資料型別間定義擴展或縮小轉換

擲回 InvalidCastException

DirectCast

任何資料型別

其中一個型別必須繼承自另一個型別或實作另一個型別

擲回 InvalidCastException

TryCast 運算子 (Visual Basic)

僅限參考型別 (Reference Type)

其中一個型別必須繼承自另一個型別或實作另一個型別

傳回 Nothing (Visual Basic)

範例

下列範例會示範 DirectCast 的兩種使用方法,其中一個會在執行階段失敗,而另一個會成功。

Dim q As Object = 2.37
Dim i As Integer = CType(q, Integer)
' The following conversion fails at run time
Dim j As Integer = DirectCast(q, Integer)
Dim f As New System.Windows.Forms.Form
Dim c As System.Windows.Forms.Control
' The following conversion succeeds.
c = DirectCast(f, System.Windows.Forms.Control)

在先前的範例中,q 的執行階段型別為 Double。 CType 會成功,因為 Double 可以轉換為 Integer。 不過,第一個 DirectCast 會在執行階段失敗,原因是 Double 的執行階段型別與 Integer 間沒有繼承關聯性,即使轉換存在也一樣。 第二個 DirectCast 會成功,原因是它會從型別 Form 轉換成型別 Control (Form 是由此繼承)。

請參閱

參考

Convert.ChangeType

概念

擴展和縮小轉換 (Visual Basic)

隱含和明確轉換 (Visual Basic)