I know the following is possible in vba:
If Len(str) Then
Len(str)
will evaluate to false
if Len(str)
is 0
, true
otherwise.
What are the exact conversion rules for vba? Please provide a link to official documentation if possible.
I know the following is possible in vba:
If Len(str) Then
Len(str)
will evaluate to false
if Len(str)
is 0
, true
otherwise.
What are the exact conversion rules for vba? Please provide a link to official documentation if possible.
A boolean
variable can have only two states, True
and False
. That's true in all programming languages (except if a nullable datatype exists, eg bool?
in C# where the variable can have also the value null
signaling it is not defined).
In VBA, a boolean is stored as 16-bit integer. False
is defined as 0 and True
as -1. Thats similar in other languages, simply because 0 has all bits set to 0 and -1 has all bits set to 1. But this is an implementation detail and your programming should not care about it. You deal with True
and False
and Boolean algebra using AND
, OR
and NOT
.
Implicit conversion rules are straight forward: Numeric data types are converted to boolean False
if they are 0 and to True
in all other cases (that is, if the value has any bit set to 1). Converting a boolean value to a number results in 0 resp. -1.
Dim i As Integer
Dim b As Boolean
i = 3
b = i ' Implicit conversion from 3 to TRUE
i = b ' Implicit conversion from TRUE to -1
Strings are converted to numbers first, and if that fails, you get a runtime error. Dates are implemented as numbers (Double) in VBA, so every date/time is True
except the date+time that is represented as 0 (that is 30 Dec 1899 00:00)
But from my point of view (and >30 years of programming), implicit conversions are evil and should be avoided. They are source of numerous errors, they lead to code that is harder to read - and there is simply no reason to rely on implicit conversations. There is no reason to write If len(str) Then
. You want to check if the length of a string is greater than 0, so write it: If len(str) > 0 Then
. Always.
Have a look to the following example: Obviously, the VBA runtime need to execute an implicit conversion, but can you tell the outcome?
Dim i As Integer
Dim b As Boolean
i = 3
b = i
If b = i Then Debug.Print "B=I"
Here you can find the official documentation from Microsoft: https://learn.microsoft.com/en-US/office/vba/language/reference/user-interface-help/boolean-data-type
I came across this in a recent textbook I was reading on C++ - I'll add a link at the end. Although a different language, both have similarities to some degree. One of these similarities is what you call 'implicit type conversion' or more technically, type casting.
For any Boolean expression in your project when you compile your VBA project (Debug > Compile VBAProject) any nonzero value will be treated as the value true
and will treat the value 0 as false
. Essentially this comes down to the how the compiler treats these statesments and also to operator precedence rules as well as boolean expression rules.
For further reading please see below links:
In C++ (as far as I am aware) this term is called copy initialisation, you can read more here:
The book that brought this deep thought recently:
Problem Solving with C++: The Object of Programming
I know this doesn't necessarily answer your question, but hopefully some of the references help.