# Ask the MultiValued Visual
Basic Expert - #13B

# (as published in Spectrum
magazine Jan/Feb 1999)

##### To email your questions to "Ask the MultiValued VB
Expert", *click here*.

Copyright © 1996-98 Caduceus Consulting. All rights reserved.

## The True/False Trap

*I read your column on your web site since I
don't get the Spectrum magazine. Thanks for all the tips and
sample code. This question may not rate publishing, but I would
really appreciate some help.*

*I wrote my own VB function called APIndex
that works like the old INDEX function, but while converting some
Data/Basic code to VB, I encountered a problem. My original code
read: "IF NOT(INDEX(CMD,"/",1)) THEN". My new
VB code reads: "If Not APIndex(Cmd$,"/",1)
Then". The problem is that the Else clause never runs,
regardless of whether Cmd$ has a "/" or not. I rewrote
the code to make it work, but I don't understand why my first
attempt did not. Can you explain it? - *Jennifer C., N.S.W.,
Australia

G'day Jennifer! No worries - your question fits
perfectly, especially because the problem, which I call the
True/False Trap, specifically targets experienced MultiValue
programmers. There is probably nothing wrong with your APIndex
function. If Cmd$ is "ABC", then APIndex returns 0, and
your "If" statement is True (because Not 0 is True). If
Cmd$ is "A/C", then APIndex returns 2, but your
"If" statement is still True (because Not 2 evaluates
to -3 which qualifies as True)! The catch is with the Not
operator.

The MultiValue NOT function is a Boolean
function: If the expression is 0 (false), it returns a 1 (true);
if the expression is non-zero (true) it returns a 0 (false). The
VB Not operator is a bitwise operator: The expression is treated
as an integer, and every bit in that integer is flipped (0 to 1,
1 to 0). 2 is stored as 0000000000000010, so Not 2 becomes
1111111111111101. Given that the first bit is the sign bit
(positive/negative) and that negative numbers count downwards,
1111111111111101 is really -3. (This also explains why the VB
concept of True is -1.)

Your red flag is the Not operator. Always
ensure that the Not operator is only applied to expressions that
can only be True or False, not other integers. This line would
work correctly for you: "If Not
APIndex(Cmd$,"/",1) <> 0 Then".

Note that the same warning applies to the VB
"And" operator. Consider the statement: "Flag =
(APosn% And BCode%)". If APosn% is 1 (true) and BCode% is -2
(true), then Flag will be False (1 And -2 equals 0)! In other
words, your code will work fine until the day that you And a
value 'n' with '-(n+1)'. Be careful!

##### To email your questions to "Ask the MultiValued VB
Expert", *click here*.

Copyright © 1996-98 Caduceus Consulting. All rights reserved.

Revised: November 10, 1998.

Return to Caduceus
Consulting Home Page