#If...Then...#Else (directive)

Syntax

#If expression Then

[statements]

[#ElseIf expression Then

[statements]]

[#Else

[statements]]

#End If

Description

Causes the compiler to include or exclude sections of code based on conditions.

Comments

The expression represents any valid BasicScript Boolean expression evaluating to TRUE of FALSE. The expression may consist of literals, operators, constants defined with #Const, and any of the following predefined constants:

 

Constant

Value

 

Win32

True if development environment is 32-bit Windows.

 

Empty

Empty

 

FALSE

False

 

NULL

Null

 

TRUE

True

 

The expression can use any of the following operators: +, -, *, /, \, ^, + (unary), - (unary), Mod, &, =, <>, >=, >, <=, <, And, Or, Xor, Imp, Eqv.

 

Following are results when an expression is evaluated.

 

Evaluates to a:

Result

 

Numeric value

Non-zero

TRUE

 

 

Zero

FALSE

 

String not convertible to a number

Type mismatch error is generated.

 

Null

Type mismatch error is generated.

 

Text comparisons within expression are always case-insensitive, regardless of the Option Compare setting

You can define your own constants using the #Const directive, and test for these constants within the expression parameter as shown below:

 #Const VERSION = 2

Sub Main

    #If VERSION = 1 Then

      directory$ ="\apps\widget"

    #ElseIf VERSION = 2 Then

      directory$ ="\apps\widget32"

    #Else

     MsgBox "Unknown version."

    #End If

End Sub

 

Any constant not already defined evaluates to Empty.

A common use of the #If...Then...#Else directive is to optionally include debugging statements in your code. The following example shows how debugging code can be conditionally included to check parameters to a function:

 #Const DEBUG = 1

Sub ChangeFormat(NewFormat As Integer,StatusText As String)

    #If DEBUG = 1 Then

     If NewFormat <> 1 And NewFormat <> 2 Then

       MsgBox "Parameter ""NewFormat"" is invalid."

       Exit Sub

End If

    If Len(StatusText) > 78 Then

       MsgBox "Parameter ""StatusText"" is too long."

       Exit Sub

     End If

    #End If

    Rem Change the format here...

End Sub

 

Excluded section are not compiled by BasicScript, allowing you to exclude sections of code that has errors or doesn’t even represent valid BasicScript syntax. For example, the following code uses the #If...Then...#Else statement to include a multi-line comment:

Sub Main

    #If 0

     The following section of code displays

     a dialog box containing a message and an

     OK button.

    #End If

    MsgBox "Hello, world."

End Sub

In the above example, since the expression #If 0 never evaluates to TRUE, the text between that and the matching #End If will never be compiled.

See Also

#Const (directive)

 

 

 

 

More information

Symbols