Conditionally compiles selected blocks of Visual Basic code.
#If expression Then statements [ #ElseIf expression Then [ statements ] ... #ElseIf expression Then [ statements ] ] [ #Else [ statements ] ] #End If
#ElseIf statements, optional elsewhere. Any expression, consisting exclusively of one or more conditional compiler constants, literals, and operators, that evaluates to
#If statement block, optional elsewhere. Visual Basic program lines or compiler directives that are compiled if the associated expression evaluates to
#If statement block.
On the surface, the behavior of the
#If...Then...#Else directives appears the same as that of the
If...Then...Else statements. However, the
#If...Then...#Else directives evaluate what is compiled by the compiler, whereas the
If...Then...Else statements evaluate conditions at run time.
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 effect on size or performance.
Regardless of the outcome of any evaluation, all expressions are evaluated using
Option Compare Binary. The
Option Compare statement does not affect expressions in
No single-line form of the
#End If directives exists. No other code can appear on the same line as any of the directives.
The statements within a conditional compilation block must be complete logical statements. For example, you cannot conditionally compile only the attributes of a function, but you can conditionally declare the function along with its attributes:
#If DEBUG Then <WebMethod()> Public Function SomeFunction() As String #Else <WebMethod(CacheDuration:=86400)> Public Function SomeFunction() As String #End If
This example uses the
#If...Then...#Else construct to determine whether to compile certain statements.
#Const CustomerNumber = 36 #If CustomerNumber = 35 Then ' Insert code to be compiled for customer # 35. #ElseIf CustomerNumber = 36 Then ' Insert code to be compiled for customer # 36. #Else ' Insert code to be compiled for all other customers. #End If
Submit and view feedback for