这些是来自 VBA https://msdn.microsoft.com/en-us/library/ee177324.aspx?f=255&MSPPError=-2147217396 的规则
关键是变量(尽管不是对象)总是有一个可用的值(对象确实没有任何值)。
5.5.1.2.2 强制转换为布尔值
不存储为布尔值时,False 用 0 表示,True 用非零值表示,通常为 -1。
Boolean Let-coercion 的语义取决于源的值类型和目标的声明类型:
Source Value Type Destination Declared Type Semantics
Boolean Boolean The result is a copy of the source value.
Boolean Any numeric type except Byte If the source value is False, the result is 0. Otherwise, the result is -1.
Boolean Byte If the source value is False, the result is 0. Otherwise, the result is 255.
Any numeric type Boolean If the source value is 0, the result is False. Otherwise, the result is True
5.5.1.2.11 Let-coercion from Empty
Empty Let-coercion 的语义取决于目标声明的类型:
Source Value Type Destination Declared Type Semantics
Empty Any numeric type The result is 0.
Empty Boolean The result is False.
Empty Date The result is 12/30/1899 00:00:00.
Empty String The result is a 0-length string.
Empty String * length The result is a string containing length spaces.
Empty Any class or Object Runtime error 424 (Object required) is raised.
Empty Any other type except Variant Runtime error 13 (Type mismatch) is raised.
当您第一次使用消息框时,您的变量被强制转换为字符串。
然后在上面一行中被强制为假。
5.6.9.5 关系运算符
关系运算符是在其操作数之间执行比较的简单数据运算符。
relational-operator = equality-operator / inequality-operator / less-than-operator / greaterthan-operator / less-than-equal-operator / greater-than-equal-operator
静态语义:
关系运算符静态解析为简单数据运算符。
如果任何操作数的声明类型是数组或 UDT,则关系运算符无效。
基于其操作数的声明类型,关系运算符具有以下声明类型:
Left Operand Declared Type Right Operand Declared Type Operator Declared Type
Any type except an array, UDT or Variant Any type except an array, UDT or Variant Boolean Any type except an array or UDT Variant
Variant Any type except an array or UDT Variant
运行时语义:
关系运算符首先被评估为简单数据运算符。
如果任何操作数的值类型是数组或 UDT,则会引发运行时错误 13(类型不匹配)。
在计算关系运算符之前,它的非 Null 操作数经过 Let-coercion 到运算符的有效值类型。
根据操作数的值类型确定有效值类型如下:
5.6.9.5.1 = 运算符
= 运算符对其操作数执行值相等比较。
等式运算符 = 表达式 "=" 表达式
运行时语义:
如果认为操作数相等,则返回 True。否则,返回 False。