傳回已轉換為子類型 Boolean 之變異的表示式。
語法
CBool(expression)
備註
表示式引數是任何有效的表示式。
如果表示式為零,則會傳回 False;否則會傳回 True。如果表示式無法解譯為數值,則會發生執行時期錯誤。
下列範例使用 CBool 函式將表示式轉換為 Boolean。如果表示式評估為非零值,CBool 會傳回 True;否則會傳回 False。
下列範例說明 CBool 函式的用法:
範例 1:比較兩個相等的數字
Dim A, B A = 5 B = 5 Check = CBool(A = B) 'Converts the expression (A = B) to Boolean 'Output: True
範例 2:測試非零值的變數
Dim A A = 1 Check = CBool(A) 'Converts the expression (A) to Boolean 'Output: True
範例 3:測試零值的變數
Dim A A = 0 Check = CBool(A) 'Converts the expression (A) to Boolean 'Output: False
範例 4:
Dim strValue strValue = "Hello" Check = CBool(Len(strValue) > 0) 'Converts the result of Len(strValue) > 0 to Boolean 'Output: True
範例 5:空字串
Dim strValue strValue = "" Check = CBool(Len(strValue) > 0) 'Converts the result of Len(strValue) > 0 to Boolean 'Output: False
範例 6:在條件陳述式中使用 CBool
Dim value value = 10 If CBool(value) Then 'It will enter in to IF part because value is non-zero 'Statements based on use case when value is non-zero Else 'Statements based on use case when value is zero End If