IfThen Else Directive

Named Arguments

Syntax

#If expression Then statements [#ElseIf furtherexpression Then

[elseifstatements]] [#Else

[elsestatements]] #End If expression

Use: Required

An expression made up of operators and conditional compiler constants that evaluate to True or False.

statements

Use: Required

One or more lines of code that are executed if expression evaluates to True.

furtherexpression Use: Optional

An expression made up of operators and conditional compiler constants that evaluates to True or False. furtherexpression is evaluated only if the preceding expression evaluates to False.

elseifstatements Use: Optional

One or more lines of code that are executed if furtherexpression evaluates to True.

elsestatements Use: Optional

One or more lines of code that are executed if expression or furtherexpression evaluates to False.

Description

Defines a block or blocks of code that are included in the compiled application only when a particular condition is met, allowing you to create more than one version of the application using the same source code. Conditionally including a block of code is a two-step process:

• Using the #Const directive to assign a value to a conditional compiler constant

• Evaluating the conditional compiler constant using #If...Then...#End If statement block.

As with the standard If...Then statement, only expressions that evaluate to True are executed directly after the statement. You can use the #Else statement to execute code when the #If...Then expression has evaluated to False. You can also use an #ElseIf statement to evaluate more expressions in the event that previous expressions in the same block have evaluated to False.

Some uses of conditional compilation code are:

• To provide blocks of debugging code that can be left within the source code and switched on and off using a conditional constant.

• To provide blocks of code that can perform different functions based on the build required by the developer. For example, you may have a sample version of your application that offers less functionality than the full product. This can be achieved using the same source code and wrapping the code for menu options, etc., within conditional compiler directives.

• To provide blocks of code that reference different ActiveX servers depending on the build criteria of the application.

Was this article helpful?

0 0

Post a comment