If . . . Then . . . Else Directive

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

Syntax

 #If   expression   Then   statements   [#ElseIf   furtherexpression   Then    [   elseifstatements   ]] [#Else    [   elsestatements   ]] #End If 
expression (required)

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

statements (required)

One or more lines of code or compiler directives, which is executed if expression evaluates to True

furtherexpression (optional)

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

elseifstatements (optional)

One or more lines of code or compiler directives, which is executed if furtherexpression evaluates to True

elsestatements (optional)

One or more lines of code or compiler directives, which are executed if expression or furtherexpression evaluates to False

Description

Defines a block or blocks of code that are only included in the compiled application 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:

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

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

Only code blocks whose expressions evaluate to True are included in the executable. You can use the #Else statement to execute code when the #If...Then expression evaluates to False . You can also use an #ElseIf statement to evaluate more expressions if 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. Since debug statements such as Debug.Write have no effect in compiled executables, they do not need to be included in conditional compilation code for the purpose of removing them from the final executable.

  • 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 components depending upon the build criteria of the application.

Rules at a Glance

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

  • All expressions are evaluated using Option Compare Text , regardless of the setting of Option Compare .

  • If a conditional compiler constant is undefined, comparing it to Nothing , , False , or an empty string ( "" ) returns True .

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 compile in all situations where ccVersion does not equal 5.

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

See Also

#Const Directive

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net