vba zero, null, or

  • Thread starter Thread starter TC
  • Start date Start date
T

TC

I avoid expressions like: 63 or null, because they rely on default
data-type coercion - and you can never be quite sure what direction the
types will be coerced in!

TC
 
?0 or Null
Null

?63 or Null
63


I didn't know that... I have a piece of code that has been working for
years, and had never had the data value 0. Today, when I tested it with zero
and Null, I discovered that Zero is treated as Boolean rather than Numeric:

?false or null
Null
?true or null
True


(david)
 
david said:
?0 or Null
Null

?63 or Null
63


I didn't know that... I have a piece of code that has been working for
years, and had never had the data value 0. Today, when I tested it with zero
and Null, I discovered that Zero is treated as Boolean rather than Numeric:

?false or null
Null
?true or null
True

Not True, David ;-))

Actually, It's logically consistent since
True OR <anything, including Null>
is true, but
False OR Null
is unknown (i.e. Null).

Another case is
False AND Null
which is False

The same kind of thing also happens with IMP where
False IMP Null
is False, and
Null IMP True
is True, but
Null IMP False
and
True IMP Null
are unknown.

I guess your point is really about VBA doing a bitwise
operations (unlike SQL and expression service) and that any
nonzero number is considered True and the Null just leaves
it at that. Suprising, maybe, but, to me at least,
consistent and at least semi-logical ;-\
 
Actually, It's logically consistent since
True OR <anything, including Null>
is true, but

Not exactly. The consistency with the Boolean vartype derives
from the underlying model of Boolean data representation, not
from logic. If the underlying data model had False = 1 and
True = 0, what answer would you expect? More to the point,
given the documentation, could I PREDICT this behaviour?
nonzero number is considered True and the Null just leaves
Not exactly :)

?vartype(true or null)
11
?vartype(-1 or null)
2


I like TC's insight: The problem of data-type coercion is more
general than the problem with overloading of the 'OR' operator
in VBA.

(david)
 
david said:
Not exactly. The consistency with the Boolean vartype derives
from the underlying model of Boolean data representation, not
from logic. If the underlying data model had False = 1 and
True = 0, what answer would you expect? More to the point,
given the documentation, could I PREDICT this behaviour?

Not exactly :)

?vartype(true or null)
11
?vartype(-1 or null)
2


I like TC's insight: The problem of data-type coercion is more
general than the problem with overloading of the 'OR' operator
in VBA.

All you say is true, I'll just plead that I think in terms
of the bitwise operations. -- You can take assembly
language away from the programmer, but you can't take the
programmer away from assembly languege ;-))
but I will try to keep this in mind.
 
Back
Top