To initialize a Boolean variable, assign it a True
or a False value. In the Visual Basic language, a Boolean variable
can also deal with numeric values. The False value is equivalent to 0. For
example, instead of False, you can initialize a Boolean variable with 0.
Any other numeric value, whether positive or negative, corresponds to
True:

As done with the other data types that we have used so far,
Boolean values can be involved with procedures. This means that a Boolean
variable can be passed to a procedure and/or a function can be made to return a
Boolean value. Some of the issues involved with procedures require conditional
statements that we will study in the next lesson. Still, the basic functionality
is possible with what we have learned so far.

Passing a Boolean Variable as Argument

To pass an argument as a Boolean value, in the
parentheses of the procedure, type the name of the argument followed by
the As Boolean expression. Here is an example:

Private Sub CheckingEmployee(ByVal IsFullTime As Boolean)
End Sub

In the same way, you can pass as many Boolean
arguments as you need, and you can combine Boolean and non-Boolean
arguments as you judge necessary. Then, in the body of the procedure, use
(or do not use) the Boolean argument as you wish.

Returning a Boolean Value

Just as done for the other data types, you can create
a function that returns a Boolean value. When declaring the function,
specify its name and the As Boolean expression on the right side of
the parentheses. Here is an example:

Public Function IsDifferent() As Boolean

Of course, the function can take arguments of any kind
you judge necessary:

Public Function IsDifferent(ByVal Value1 As Integer, _
ByVal Value2 As Integer) As Boolean

In the body of the function, do whatever you judge
necessary. Before exiting the function, you must return a value that
evaluates to True or False. We will see an example
below.

Boolean Built-In Functions

Converting a Value to Boolean

To assist you with validating some values or variables to
true or false, the Visual Basic language provides many functions. First, to convert a value to Boolean, you can use
the CBool() function. Its syntax is:

Function CBool(ByVal Expression As Variant) As Boolean

Like all conversion functions, CBool takes one argument, the
expression to be evaluated. It should produce a valid Boolean value. If it does,
the function returns True or False.

Checking Whether a Variable Has Been Initialized

After declaring a variable, memory is reserved for but you should
assign value to it before using it. At any time, to check whether a variable has
been initialized, you can call the IsEmpty() function.
Its syntax is:

Public Function IsEmpty(ByVal Expression As Variant) As Boolean

When calling this function, pass the name of a variable to it. If the
variable was already initialized, the function would return True.
Otherwise, it would return False.

Checking Whether a Value is Numeric

One of the most valuable operations you will perform on a
value consists of finding out whether it is numeric or not. To assist you with
this, the Visual Basic language provides a function named IsNumeric. Its
syntax is:

Public Function IsNumeric(ByVal Expression As Variant) As Boolean

This function takes as argument the value or expression to
be evaluated. If the argument holds or can produce a valid integer or a decimal
value, the function returns True. Here is an example:

After declaring a variable, you should initialize it
with a valid value. Sometimes you will not. In some other cases, you may
be using a variable without knowing with certainty whether it is holding a
valid value. To assist you with checking whether a variable is currently
holding a valid value, you can call the IsNull() function. Its
syntax is:

Public Function IsNull(ByVal Expression As Variant) As Boolean

When calling this function, pass the name of a variable
to it. If the variable is currently holding a valid value, this function
would returns True. Otherwise, it would return False.

Logical Operators

Introduction

A comparison is an operation used to get the boolean
result of two values one checked against the other. Such a comparison is
performed between two values of the same
type.

Equality

To compare two variables for equality, use the =
operator. Its syntax is:

Value1 = Value2

The equality operation is used to find out whether two
variables (or one variable and a constant) hold the same value. From our
syntax, the value of Value1 would be compared with the value of Value2. If
Value1 and Value2 hold the same value, the comparison produces a True
result. If they are different, the comparison renders false or 0.

As opposed to checking for equality, you may instead
want to know whether two values are different. The operator used to
perform this comparison is <> and its formula is:

Variable1 <> Variable2

If the operands on both sides of the operator are the
same, the comparison renders false. If both operands hold different
values, then the comparison produces a true result. This also shows that
the equality = and the inequality <> operators are opposite.

To find out whether one value is lower than another, use the
< operator. Its syntax is:

Value1<Value2

The value held by Value1 is compared to that of Value2. As it
would be done with other operations, the comparison can be made between two
variables, as in Variable1 < Variable2. If the value held by Variable1 is
lower than that of Variable2, the comparison produces a True.

The previous two operations can be combined to compare two
values. This allows you to know if two values are the same or if the first is
less than the second. The operator used is <= and its syntax is:

Value1<=Value2

The <= operation performs a comparison as any of the last
two. If both Value1 and VBalue2 hold the same value, result is true or positive.
If the left operand, in this case Value1, holds a value lower than the
second operand, in this case Value2, the result is still true:

Greater Value >

When two values of the same type are distinct, one of them is
usually higher than the other. VBasic provides a logical operator that
allows you to find out if one of two values is greater than the other. The
operator used for this operation uses the > symbol. Its syntax is:

Value1 > Value2

Both operands, in this case Value1 and Value2, can be
variables or the left operand can be a variable while the right operand is a
constant. If the value on the left of the > operator is greater than the
value on the right side or a constant, the comparison produces a True
value. Otherwise, the comparison renders False or null:

The greater than or the equality operators can be combined to
produce an operator as follows: >=. This is the "greater than or equal
to" operator. Its syntax is:

Value1 >= Value2

A comparison is performed on both operands: Value1 and
Value2. If the value of Value1 and that of Value2 are the same, the comparison
produces a True value. If the value of the left operand is greater than
that of the right operand, the comparison still produces True. If the
value of the left operand is strictly less than the value of the right operand,
the comparison produces a False result: