What are the Integer values of Boolean False and True in VB6?

前端 未结 3 789
南方客
南方客 2020-12-19 01:52

I\'m working with a bit of old VB6 code that goes thus...

Dim STATUS As Integer

STATUS = -1

If (Not STATUS) Then
\' do something
Else
\' do something else
         


        
相关标签:
3条回答
  • 2020-12-19 02:17

    Not really an answer, but just poking about, I typed this into the immediate window, with these results:

    For x = -5 To 5 : ? x, CBool(x), ( x = True ), ( x = False ) : Next x
    -5            True          False         False
    -4            True          False         False
    -3            True          False         False
    -2            True          False         False
    -1            True          True          False
     0            False         False         True
     1            True          False         False
     2            True          False         False
     3            True          False         False
     4            True          False         False
     5            True          False         False
    

    (I tested more values, but only -1 and 0 had anything "interesting" going on. The rest were all True/False/False.) So, empirically, I'd say that the comparison is being done arithmetically unless you cast with CBool. Why? I can't really say...

    0 讨论(0)
  • 2020-12-19 02:20

    In VB 6, True has a numeric value of -1. False has a numeric value of 0.

    The reason for this is because the Boolean data type is stored as a 16-bit signed integer. Therefore,
    -1 evaluates to 16 1s in binary (1111111111111111). False is 16 0s (0000000000000000). This produces the relationship that has held throughout the evolution of BASIC: True = Not False.

    0 讨论(0)
  • 2020-12-19 02:22

    True is stored as -1 and false as 0. Any non-zero value is considered as true.

    To see why it is so please check - http://www.vbforums.com/showthread.php?t=405047

    0 讨论(0)
提交回复
热议问题