For the past several months, I've been unfortunate enough to do lots of programming in VB6. In that time, I've learned a lot of little quirks. Some specific to VB, some relative to programming in general.
Contrary to what is stated above, it will not fail if you just use
If Method(arg) Then. Only the return value 0 will evaluate to False, and anything else (including 1 and -1) will evaluate to True.
It is preferable to return a
boolean from a method when control structures depend on that return value, but there are alternative techniques. If the method's return value has to be a number that is actually used for other things, and you just happen to be using it in a conditional statement, it's better to explicitly compare the return value to some other value.
If Method(arg) > 0 Then
'things are pretty clear this way
If Method(arg) <> -1 Then
'this, too, easily readable
As far as
True being equal to -1, it may seem strange, but it makes a little sense if you understand a few other things. All logical operators in VB (And, Or, Xor, Not, whatever) are bitwise. In VB, the expression
7 And 8 will result in
0 because 7 is 00000111 and 8 is 00001000. Neither have any bits in common that are on, so the result is 00000000.
In other languages, there are both the bitwise operators (&, |, ^, ~) which of course operate on the bits of a value, and the logical operators (&&, ||, !) which only operate on boolean values.
Boolean type in VB does not store its value in a single bit, but in a byte, and VB also has no unsigned integer types. Hopefully you've already thought ahead and realized what happens, but here's the long and short of it:
Not False is the same as
Not is bitwise
Not 00000000 is
11111111, which in a signed integer is -1.
They had to make
True equivalent to -1 because if it was merely 1 (00000001), then
Not True would evaluate to -2 (11111110). This happens with any signed integer, not just the 8-bit variety.
Another funny thing with -1 is something a coworker accidentally did and could not figure out (although he has a Masters Degree in Computer Science and should have been seen it coming). He's writing a program to pull data from one database and stick it in another (both are different types and structures), and there was an integer column where some values were -1. After he did a test conversion, he was verifying the data, but found that the fields that were previously -1 were now 18446744073709551615.
It tried to assign -1 to the field, right? Right, but there's a snag. A signed integer with the value -1 means all the bits are 1 instead of 0. So what happens when you try to assign a value with all bits on to an unsigned integer? You get that integer's highest possible value because Two's Complement has nothing to do with unsigned integers, so all bits set to 1 just means the highest possible value, and a 64-bit integer's highest value is 18446744073709551615.