Boolean in if statement without a value

Wysy

Registered User.
Local time
Yesterday, 22:27
Joined
Jul 5, 2015
Messages
339
Hi,
I have just came across a code i used successfuly, but would like to ask for a bit of interpretion for curiosity
Code:
dim ss as boolean
    if ss then
        msgbox
    else
        msgbox
    end if

This code gives the same result than the
Code:
 if ss=true then

        msgbox

    else

        msgbox
    end if

So am i correct to suppose that if no value assigned (omit =) i.e. no '=' than the default value is used that is false?
Thank you!
 
Sorry i was giving confusing information. So the boolean is missing =true than it is handled automatically false.Correct?
 
In vba all numeric types (Boolean is numeric) have a default of 0 on declaration, strings a default of “”. For Booleans, 0=false, -1=true - so you can use them in calculations if you want

Dates are null, variants and objects are nothing

Not declaring a type defaults to variant
 
One more question please. The code
Code:
if ss then
        msgbox
    else
        msgbox
    end if
functions like if ss=true then the first condition, if ss=false then the second condition performed. Is this the automatic designed logic with boolean values not provided in the first line of the if statement?
 
Don’t understand the question - doesn’t help that your code returns the same value regardless of true or false
 
functions like if ss=true then the first condition, if ss=false then the second condition performed. Is this the automatic designed logic with boolean values not provided in the first line of the if statement?
First or second doesn't matter. You should understand the concept.
You can also do it this way :
SQL:
If Not SS Then
    Msgbox "SS is False"
Else
    Msgbox "SS is true"
end If
 
Your code as is, without assigning the value of ss before reading it, will go down the false path. The variable ss will default to false when dimensioned.

This will show you more clearly:
Code:
Dim ss As Boolean

if ss then
        msgbox "ss is True"
    else
        msgbox "ss is False"
    end if
 
Part of this is tied up with understanding the way the variables work. We often talk about how a DATE variable is actually a DOUBLE for which special treatment occurs. This concept is called a TYPECAST - meaning you store something as though it were something else, but then provide special treatment of the "underlying" i.e. base variable, usually related to interpretation or formatting of the variable's value. VBA does this special treatment based on the way the variable was declared. We always recommend Option Explicit for every VBA module because that way, VBA knows when we want special treatment (or, for that matter, ordinary treatment) for each variable. See also the last paragraph, regarding VARIANT variables and how they sneak into your code.

So, your question was about Booleans. Technically, a declared i.e. DIM'd Boolean variable is a typecast of an INTEGER. The special treatment is that when the underlying integer is 0, that equates to the Boolean value FALSE, and when the integer is NOT 0, then it represents TRUE. Yes, that means that one and only one INTEGER value is truly FALSE and all of the other 65535 possible INTEGER values are TRUE. It's a side-effect of being a typecast. Since the Boolean, at its base, is an INTEGER, it initializes like an INTEGER. The VBA default value for a declared INTEGER is 0 but because of the typecast, that value is treated as FALSE. For comparison, VBA DATE variables - being a DOUBLE at base - initialize as 0.0, which is then treated as the "reference date" of midnight, 30-Dec-1899.

That special treatment actually works both ways because you can perform CBOOL(integer) and CINT(Boolean) functions to interconvert them. In the case of IF statements, the IF requires a Boolean value. If you give the IF statement an INTEGER, a process called "LET Coercion" converts integers to Booleans "behind the scenes", thus leading to the IF integer case working smoothly. Using the IF x=y construct, the x=y expression is a relational expression which evaluates to a Boolean internally, so that works properly.

The "fly in the ointment" is when you DON'T use Option Explicit and fail to declare the variable. The default data type for undeclared variables is a VARIANT, and the default value for a VARIANT is NULL. Depending on other settings related to error handling and suppression, that NULL could cause either an "Invalid use of null" error OR the variable could behave as if it were always FALSE. In the worst possible case of error suppression, you would find that the expressions IF a-null-value and IF NOT a-null-value would behave identically. That is because NULL "poisons" any expression containing it.
 
the default value for a VARIANT is NULL
Actually, this is not true. I used to think so for years until it tripped me up!

A Variant defaults to Empty.

Try the following in the Immediate Window (no need to declare variable 'test', it is a Variant by default):
Code:
?IsNull(test)
False
?IsEmpty(test)
True
 
In vba all numeric types (Boolean is numeric) have a default of 0 on declaration, strings a default of “”. For Booleans, 0=false, -1=true - so you can use them in calculations if you want

Dates are null, variants and objects are nothing

Not declaring a type defaults to variant
Dates are zero:
Dim dt As Date
Debug.Print dt, CLng(dt), IsNull(dt), Year(dt), Month(dt), Day(dt)

12:00:00 AM 0 False 1899 12 30
 
Hi,
I have just came across a code i used successfuly, but would like to ask for a bit of interpretion for curiosity
Code:
dim ss as boolean
    if ss then
        msgbox
    else
        msgbox
    end if

This code gives the same result than the
Code:
 if ss=true then

        msgbox

    else

        msgbox
    end if

So am i correct to suppose that if no value assigned (omit =) i.e. no '=' than the default value is used that is false?
Thank you!
If I understood your question correctly, I think the value assignment is a red herring. You may be focusing on the wrong issue. For example, your title states that the boolean in your if statement is "without a value." But yet, in the last statement in your original post, you acknowledged that you knew it had a value - the default value of false.

We all know the result of the following code.
Code:
Dim ss As Boolean

If ss Then
    MsgBox "True"
Else
    MsgBox "False"
End If
But if you do it this way, the result will be different.
Code:
Dim ss As Boolean

ss = True

If ss Then
    MsgBox "True"
Else
    MsgBox "False"
End If
Notice that there is no "ss=" in both versions of the If statement, and yet the results were different.

The reason for that is because an If statement needs to evaluate an expression to a boolean value - that's either True or False. And since ss was already a boolean, it's acceptable to use a shorthand method in the If statement, because the end result is either True or False.

Hope that makes sense...
 
X = Y
will result in a boolean. The two variables are equal or they're not. Think of it as "If True Then..." Then you just substitute a boolean variable and it does the same thing

If blnTrue Then
...
it's just 201 level shorthand
 

Users who are viewing this thread

Back
Top Bottom