0

I just want to understand why the following scenario is happening:

Msgbox True
Output: True

Msgbox -True
Output: 1

Why does it become 1?

BigBen
  • 46,229
  • 7
  • 24
  • 40
inter cretasyo
  • 149
  • 1
  • 12
  • Does this answer your question? [What are the Integer values of Boolean False and True in VB6?](https://stackoverflow.com/q/4275800/692942) – user692942 Apr 16 '21 at 12:52
  • 3
    @user692942 VBA, VB6, VB.Net and VBScript are all different. Please don't add tags that OP did not intend. – BigBen Apr 16 '21 at 12:55
  • @BigBen when it comes to how they handle Boolean values, they are all the same. Let's be clear the OP doesn’t know what they intended as they added both VBA and VBScript. – user692942 Apr 16 '21 at 13:40
  • @user692942 - I agree the question isn't clear due the inclusion of both VBA and VBScript tags. However, I believe one should have asked OP for clarification, rather than adding more tags to then propose VB6 and VB.Net dupe targets, which *weren't* the original tags. Also, we see wayyy too questions with any such combination of VB6, VBA, VBScript, and VB.Net... Let's not cause more confusion. – BigBen Apr 16 '21 at 13:55

1 Answers1

5

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?

FunThomas
  • 23,043
  • 3
  • 18
  • 34
  • @VBasic2008: Of course you're right. Corrected. – FunThomas Apr 16 '21 at 10:58
  • 1
    Worth to mention that this applies to VBA only where `False ≙ 0` and `True ≙ -1` but when it comes to Excel formulas it is `False ≙ 0` and `True ≙ 1` (positive not negative `1`). So actually where ever you are the **only** definition is `False = 0` and then `True` is defined as `Not False`. If you declare `Dim b As Boolean` and set `b = 5` this will turn to `True` so anything that is not `0` will be considered `True`. If you want to test numeric, then instead of testing for `-1` or `1` for `True` test for `Not 0` or `<> 0` and you are safe because that is what is actually the definition. – Pᴇʜ Apr 16 '21 at 11:15
  • Thank you @FunThomas, I understand now. Also, just trying Msgbox Int(True) will also show that the value is -1. Again, thank you so much. – inter cretasyo Apr 16 '21 at 11:26
  • 3
    @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`. – Pᴇʜ Apr 16 '21 at 11:28
  • Hi @Pᴇʜ, yes, I understand this now too. Thank you. It's just that initially, I always thought that True is 1 and False is 0 for VBA. – inter cretasyo Apr 16 '21 at 11:36
  • @intercretasyo That's theoretically spoken not so wrong as `cBool(1)` is `True` too in VBA. It just does not work the other way round. You really need to see it as `False = 0` and `True <> 0`. So what I meant to say is you should write your code in a way that it works no matter what the result of `cInt(True)` is. That can be done by testing `If cInt(True) <> 0` which is much better then `If cInt(True) = -1` because it considers how `True` actually is defined. This is very theoretical but the code that test for `<>0` would work in any programming language while testing for `=-1` might fail. – Pᴇʜ Apr 16 '21 at 11:46
  • 4 upvotes for an answer to a question that is a clear [duplicate of a 20 year old question](https://stackoverflow.com/questions/3621037/casting-a-boolean-to-an-integer-returns-1-for-true). There is nothing new here. – user692942 Apr 16 '21 at 12:48
  • 2
    @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. – FunThomas Apr 16 '21 at 12:51
  • @FunThomas so add to the problem, makes perfect sense. – user692942 Apr 16 '21 at 12:53
  • You're right. But the alternative would be to not answer at all. Then either someone else does it (and I have seen too many low level answers lately) or the OP don't get any answer. What do you suggest? – FunThomas Apr 16 '21 at 12:57
  • @FunThomas Flag / Close it as a duplicate. – user692942 Apr 16 '21 at 13:42
  • 3
    @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. – FunThomas Apr 16 '21 at 14:16