Home

Boolean Values

 
  

Fundamentals of Boolean Values

 

The Boolean Data Type

A value is referred to as Boolean if it can be either true or false. Like a number or a string, a Boolean value can be stored in a variable. To declare such a variable, use the Boolean keyword. Here is an example:

Public Module Exercise

    Public Function Main() As Integer
        Dim EmployeeIsMarried As Boolean

        Return 0
    End Function

End Module 

To actually use a Boolean variable, you can assign a value to it. By default, if you declare a Boolean variable but do not initialized it, it receives a value of False:

Public Module Exercise

    Public Function Main() As Integer
        Dim EmployeeIsMarried As Boolean

        MsgBox("Employee Is Married? " & EmployeeIsMarried)
        Return 0
    End Function

End Module

This would produce:

Boolean Variable

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:

Public Module Exercise

    Public Function Main() As Integer
        Dim EmployeeIsMarried As Boolean

        EmployeeIsMarried = -792730
        MsgBox("Employee Is Married? " & EmployeeIsMarried)
        Return 0
    End Function

End Module

The number can be decimal or hexadecimal:

Public Module Exercise

    Public Function Main() As Integer
        Dim EmployeeIsMarried As Boolean

        EmployeeIsMarried = &HFA26B5
        MsgBox("Employee Is Married? " & EmployeeIsMarried)
        Return 0
    End Function

End Module

Passing a Boolean Variable as Argument

As done with the other data types we have used so far, a Boolean values can be involved with a procedure. This means that a Boolean variable can be passed to a procedure and/or a function can be made to return a Boolean value.

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 don't use) the Boolean argument.

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 closing parenthesis. Here is an example:

Public Function IsDifferent() As Boolean
    
End Function

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
    
End Function

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.

 
 
     
 

Home Copyright © 2008-2010 FunctionX, Inc.