Table of contents
TOC
Collapse the table of content
Expand the table of content

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

office 365 dev account|Last Updated: 1/9/2018
|
2 Contributors

Conditionally compiles selected blocks of Visual Basic code.

Syntax

#IfexpressionThen
statements
[ #ElseIfexpression-nThen
[ elseifstatements ]]
[ #Else
[ elsestatements ]]
#End If

The #If...Then...#Else directive syntax has these parts:

PartDescription
expressionRequired. Any expression, consisting exclusively of one or more conditional compiler constants, literals, and operators, that evaluates to True or False.
statementsRequired. Visual Basic program lines or compiler directives that are evaluated if the associated expression is True.
expression-nOptional. Any expression, consisting exclusively of one or more conditional compiler constants, literals, and operators, that evaluates to True or False.
elseifstatementsOptional. One or more program lines or compiler directives that are evaluated if expression-n is True.
elsestatementsOptional. One or more program lines or compiler directives that are evaluated if no previous expression or expression-n is True.

Remarks The behavior of the #If...Then...#Else directive is the same as the If...Then...Else statement, except that there is no single-line form of the #If, #Else, #ElseIf, and #End If directives; that is, no other code can appear on the same line as any of the directives. Conditional compilation is typically used to compile the same program for different platforms. It is also used to prevent debugging code from appearing in an executable file. Code excluded during conditional compilation is completely omitted from the final executable file, so it has no size or performance effect. Regardless of the outcome of any evaluation, all expressions are evaluated. Therefore, all constants used in expressions must be defined — any undefined constant evaluates asEmpty.

Note The Option Compare statement does not affect expressions in #If and #ElseIf statements. Expressions in a conditional-compiler directive are always evaluated with Option Compare Text.

Example

This example references conditional compiler constants in an #If...Then...#Else construct to determine whether to compile certain statements.

' If Mac evaluates as true, do the statements following the #If. 
#If Mac Then 
 '. Place exclusively Mac statements here. 
 '. 
 '. 
' Otherwise, if it is a 32-bit Windows program, do this: 
#ElseIf Win32 Then 
 '. Place exclusively 32-bit Windows statements here. 
 '. 
 '. 
' Otherwise, if it is neither, do this: 
#Else 
 '. Place other platform statements here. 
 '. 
 '. 
#End If
© 2018 Microsoft