If...Then...Else Directive


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

Syntax

     #If expression Then        statements     [#ElseIf expression Then        [statements]]     [#Else        [statements]]     #End If 


expression (required)

An expression made up of literals, operators, and conditional compiler constants that evaluates to true or False


statements (required for #If block)

One or more lines of code or compiler directives

Description

The #If...Then...#Else directive defines blocks of code that are only included in the compiled application when a particular condition is met or not 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:

  1. Define the conditional compiler constant through the #Const directive, the project properties, or command-line compiler switches (see Appendix H).

  2. Evaluate the constant with a #If...Then directive block. If a particular compiler constant is referenced in a #If...Then directive but is not defined, it has value of Nothing.

Only code blocks with expressions that evaluate to TRue are included in the executable. You can use the #Else statement to include code when none of the conditions are met. Use the #ElseIf portion any number of times to evaluate more conditions.

Conditional compilation blocks can be used to include or exclude debugging code. Such code can be excluded from a release compilation of the project. They can also be used to conditionally include code in different editions of your application. For instance, your project may result in two outputs, one with limited features available at a lower price to your customers and one with more advanced features.

Usage 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, 0, False, or an empty string ("") returns TRue.

  • The Visual Basic compiler includes several predefined compiler constants for your use. See the "Conditional Compilation Constants" section of Appendix H for a listing of these constants.

Example

     #Const UseAdvancedSet = True     Private monitoredSet As Object     Public Sub MonitorCorrectSet(  )     #If UseAdvancedSet = True Then        monitoredSet = New MyObject.AdvancedSet     #Else        monitoredSet = New MyObject.BasicSet     #End If     End Sub 

Only one of the assignment statements will appear in the compiled output; for the line that is not included, it will be as if the source code never included it at all.

See Also

#Const Directive




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

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