Why does yes = -1?

  • Thread starter Thread starter Kevin Zubrus
  • Start date Start date
but isn't -1(0+0) = 0?
and why does x = no-no?
-----Original Message-----
x = yes
0 = no
x = no - no
yes = no - no
yes = -1(no + no)
yes = -1(0 + 0)
yes = -1




.
 
Kevin Zubrus said:
No particular reason, I'd just like to know why yes = "-1"
and not "1"

It's arbitrary behavior defined by the Jet Database Engine and by VBA.
Boolean (yes/no, true/false) values are minimally represented by one
bit, 0 for false, 1 for true. Boolean values in Jet and VBA are both
stored internally as 2-byte integers. In the format used for
representing a two-byte integer, a value of 0 is stored, in binary, as
0000000000000000; a value of -1 is stored as 1111111111111111. As you
can see, these values are about as opposite as they can get -- I think
that was the basis of the decision made by the developers and language
designers.

It may help to realize that, in VBA, a conditional expression is
actually evaluated on the basis of zero/nonzero. So although the
constant True is equal to -1, logic like this works:

Dim intVariable As Integer

intVariable = 1234

If intVariable Then
Msgbox "Hey! That's true!"
End If

Note these results, though:

intVariable = 1234

If intVariable = True Then
Msgbox "You'd think that might be true ..."
Else
Msgbox "... but you find that it isn't."
End If

That's because 1234 doesn't *equal* the constant True, though it is
*logically* true.
 
Execellent little answer Dirk!

I will add to this:

if intVarabile then

What happens is that VBA casts the value to a Boolean. In fact you get:


cbool(intVarabile)


And, in fact, when comparing the same types, you get:

If cbool(intVariable) = True Then
msgbox "yes...this is true!"

if intVariable is any non zero value, then it is consider to return a value
of true.
 
Back
Top