I just want to understand why the following scenario is happening:
Msgbox True
Output: True
Msgbox -True
Output: 1
Why does it become 1?
I just want to understand why the following scenario is happening:
Msgbox True
Output: True
Msgbox -True
Output: 1
Why does it become 1?
Because you force VBA to implicitly convert a boolean to a number. True
is internally represented as -1, so converting it to a number will result in -1, and -(-1)
is 1
. False
, btw, is stored as 0, so -False
will print 0
.
However, you should avoid to do calculations with a boolean. If you do so, it is very likely that you go a wrong path. A boolean can be true or false, that's all you need to know. Only thing you should use is Bool'sche Algebra using AND
, OR
and NOT
Microsoft VBA documentation about Boolean - see also What are the implicit type conversion rules in vba?