#If...Then...#Else Directive

Named Arguments

No

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 expressionevaluates 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.

Sidebar 2. #If...Then and Platform

In Visual Basic 4.0, which included both 16- and 32-bit versions, the major application of the #If...Then compiler directive was to generate separate executables for the 16- and 32-bit Windows platforms. For this purpose, VBA included two conditional compiler constants, Win16 and Win32; if one was True, the other was automatically set to False. Visual Basic 5.0 and VBA 5.0 onwards, however, support only the 32-bit Windows platforms. Consequently, the Win16 and Win32 constants are no longer supported.


Rules at a Glance

  • According to the documentation, only operators (other than Is) and conditional compiler constants can be used in the expression to be evaluated. In fact, you can draw on a considerably broader range of the VBA language to evaluate a conditional compiler expression. In addition to these, you can use literals, variables, and some functions.

  • Unlike the normal If...Then statement, you can't use a single-line version of the #If...Then statement.

  • All conditional compiler constants used in conditional compiler expressions must be defined; otherwise, they evaluate to Empty. This, in turn, means that the conditional compiler expression evaluates to False.

Example

#Const ccVersion = 2.5 
Private oTest as Object

Sub GetCorrectObject()     

#If ccVersion = 2.5 Then
   Set oTest = New MyObject.MyClass
#Else
   Set oTest = New MyOtherObject.MyClass
#End If
         
End Sub

Programming Tips and Gotchas

  • You can negate the evaluation of the expression in the #If...Then or #ElseIf...Then statements by placing the Not operator before the expression. For example, #If Not ccVersion = 5 Then forces the code after this line to be compiled in all situations where ccVersion doesn't equal 5.

  • Conditional compilation helps you debug your code, as well as provide a way to create more than one version of your application. You can include code that operates only when run in debug mode. The code can be left in your final version and won't compile unless running in the debugger; therefore, you don't need to keep adding and removing code.

  • That you can use a wider range of language elements without generating a compiler error doesn't necessarily mean that you should use them or that using them produces the result that you want. This applies to the use of variables in particular; the distinguishing feature of a variable (and the reason for its name) is that its value is allowed to vary at runtime. The evaluation of conditional expressions, however, occurs at compile time.

See Also

#Const Directive, Debug.Print Method, If...Then Statement
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.145.122