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 somethi开发者_运维百科ng else
End If
so I was, naturally, wondering which branch of this code is executed. So does anyone know what the numeric values of True and False are in VB6?
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
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
.
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...
精彩评论