How come both IntelliSense and the compiler accepts If 3 = True Then ...
in VB.NET? Even with Option Strict on.
开发者_如何学CDoes it in actuality treat Booleans as Integers, or what's the deal?
From MSDN, Boolean Data Type (Visual Basic):
When Visual Basic converts numeric data type values to Boolean, 0 becomes False and all other values become True.
So, any number that is converted to boolean evaluates to True
, apart from 0.
Any non-zero integer value = Boolean True.
Comparisons of integer values can be used in boolean expressions.
True is equated as any non-zero value. You should receive the same response with
If -3 = True Then
The condition If 3 = True Then
compiles because Boolean
values can be implicitly converted to Integer
. So when it sees you comparing an Integer
and a Boolean
it will implicitly convert True
to the Integer
of -1
(and would convert False
to 0
) and then do the comparison -1 = 3
. It will not, as I feel other answers suggest, convert 3
implicitly to a Boolean
value and then do a Boolean
comparison.
In Visual Basic, If <Integer> Then
is different from If <Integer> = True Then
. You can do a conversion to Boolean
explicitly using CBool
on the Integer
value. If you do that, If CBool(3) = True Then
is a Boolean
comparison.
@Oded "L - at a guess, this is probably because VB6/VBA behave this way, and keeps developers used to those languages in their happy zone. – Oded 2 days ago "
Actually, my understanding of the situation is that Visual Basic 6.0 and VBA were at odds with the rest of the programming community in their treatment of booleans, in that for those two languages, true
is represented by -1
instead of +1
. The move to "any value other than 0 = true" was for backward compatibility.
I believe that for most of the world, the important property is the 0
equals False
. Any value other than 0
is considered to be True
.
While in many cases, I am betting that the standards are that 0
represents “false” and 1
represents “true”, when Microsoft was developing VB.NET, they needed to retain backward compatibility for the -1
equals true
holdover from Visual Basic 6.0/VBA. Therefore, they went with the True <> 0
. In this way, either 1
or -1
would resolve to True
. This seems cleaner to implement than a more complex conditional statement which covers both cases of either 1
or -1
. What they landed with is essentially a straight evaluation checking for any value other than 0
.
If you think about it, this seems consistent with basic binary concepts as well. The switch is on (some value other than 0
) or off (value is 0
).
It is worse than that. In VBA 10 * TRUE = -10 and 10 * FALSE = 0. Try error trapping a boolean variable that does not belong in the slot for an integer function argument. I tried multiple ways and failed. Check out the Excel MATCH function and put a boolean in the argument for match condition which is supposed to be either -1,0 or 1. It accepts the boolean and runs with it. A serious VBA bug IMO.
I did find a work around. Declare the function argument that is supposed to be an integer as variant. Then convert the passed argument to a string and do a conditional string comparison to "True" and "False". If you get a match then you have flushed out the boolean and can raise an error.
精彩评论