Tuesday, June 03, 2008

True / False in classic VB

In classic VB (and classic ASP as well), zero is considered as false and any other non-zero integer is considered as true.

While debugging an asp page, I come across following line:

If Not Request.Form.Count Then

Now form count was 2 (true) so the condition should evaluate as false (Not true). But this was always evaluating as true. It took me half an hour to find the catch.

What actually happening here was that Not is a bit-wise operator in VB. It flips the bits. So,

2 is 0000 0010 in binary.
NOT 2 is 1111 1101 in binary.

Considering that the last bit is sign bit, this becomes -3.

Now our condition If NOT (2) becomes If (-3), which always evaluates to true (non-zero integers are always true in VB).


Technorati Tags: ,,,

Related Posts