CBool 函数

返回已转换为子类型 Boolean 的变体的一个表达式。

语法

CBool(expression)

注释

expression 参数是任何有效的表达式。

如果 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