为什么visualbasic中的-true变成1

我只想了解为什么会发生以下情况:

Msgbox True
Output: True

Msgbox -True
Output: 1

为什么会变成1?

回答

因为您强制 VBA 将布尔值隐式转换为数字。True在内部表示为 -1,因此将其转换为数字将导致 -1,并且-(-1)1False,顺便说一句,存储为 0,因此-False将打印0.

但是,您应该避免使用布尔值进行计算。如果你这样做,很可能你走错了路。布尔值可以为真或为假,这就是您需要知道的全部内容。你唯一应该使用的是 Bool'sche Algebra using AND,ORNOT

关于布尔值的Microsoft VBA 文档- 另请参阅vba 中的隐式类型转换规则是什么?

  • @intercretasyo But actually any number than `0` produces `True`. Test `cBool(5)` or replace `5` with any number than `0` and it will return `True`. Note that `True` will return `-1` but that's not the definition. The definition of `True` is not beeing `0`.
  • @user692942:I know, but I wasted already so much time on SO to look for duplicates which is a frustrating thing. The best fitting question never got an accepted answer, a fitting answer has a slightly different focus, searching on SO gives you 100s of hits that you need to go thru to find a match - you name it. BTW: The answer that is now used for the closing reason is obviously a wrong link as it refers to VB.Net, not VBA.
  • @user692942 Yes I know. I am just sooo tired to search in 200k questions for the best fitting to mark a question as duplicate when I can answer it within a minute... And I didn't expect that is gets any upvotes, but is seems the more time you spend on an answer the less likely it is to get any reputation points.

以上是为什么visualbasic中的-true变成1的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>