檢查表達式結果的運行時間類型是否與指定的類型相容。
語法
result = TypeOf objectexpression Is typename
result = TypeOf objectexpression IsNot typename
組件
result
返回。
Boolean 值。
objectexpression
必須的。 評估為參考型別的任何表達式。
typename
必須的。 任何數據類型名稱。
備註
運算子 TypeOf 會判斷的 objectexpression 運行時間類型是否與 typename相容。 相容性取決於的 typename型別類別。 下表顯示如何判斷相容性。
類型類別目錄 typename |
相容性準則 |
|---|---|
| 班級 |
objectexpression 的類型為 typename 或繼承自 typename |
| 結構 |
objectexpression 的類型為 typename |
| 介面 |
objectexpression 實作 typename 或繼承自實作的類別 typename |
如果的 objectexpression 執行時間類型符合相容性準則, result 則為 True。 否則,result 為 False。 如果 objectexpression 為 null, 則 TypeOf...Is 會傳 False回 ,而 ...IsNot 會傳 True回 。
TypeOf一律搭配 Is 關鍵詞來建構 ...Is 表達式,或搭配 IsNot 關鍵詞來建構 TypeOfTypeOf...IsNot 表達式。
範例
下列範例會使用 TypeOf...Is 表達式來測試兩個對象參考變數與各種數據類型的類型相容性。
Dim refInteger As Object = 2
MsgBox("TypeOf Object[Integer] Is Integer? " & TypeOf refInteger Is Integer)
MsgBox("TypeOf Object[Integer] Is Double? " & TypeOf refInteger Is Double)
Dim refForm As Object = New System.Windows.Forms.Form
MsgBox("TypeOf Object[Form] Is Form? " & TypeOf refForm Is System.Windows.Forms.Form)
MsgBox("TypeOf Object[Form] Is Label? " & TypeOf refForm Is System.Windows.Forms.Label)
MsgBox("TypeOf Object[Form] Is Control? " & TypeOf refForm Is System.Windows.Forms.Control)
MsgBox("TypeOf Object[Form] Is IComponent? " & TypeOf refForm Is System.ComponentModel.IComponent)
refInteger變數的執行時間類型為 Integer。 它與相容, Integer 但與 不相容 Double。
refForm變數的執行時間類型為 Form。 它與 Form 相容,因為 是它的型別,Control因為 Form 繼承自 Control,而 IComponent 因為 繼承自 Component,而 會Form實作 IComponent。 不過, refForm 與不相容 Label。