#If...Then...#Else Directives

#If...Then...#Else Directives

Conditionally compiles selected blocks of Visual Basic code.

#If expression Then
   statements
[ #ElseIf expression Then
   [ statements ]
...
#ElseIf expression Then
   [ statements ] ]
[ #Else
   [ statements ] ]
#End If
Parts
expression
Required for If and ElseIf statements, optional elsewhere. Any expression, consisting exclusively of one or more conditional compiler constants, literals, and operators, that evaluates to True or False. Three conditional compilation constants are provided: Config, Debug, and Trace. Debug and Trace are Boolean datatypes and can be set in the Project Properties dialogue. When Debug is defined, Debug class methods generate output to the Output window. When it is not defined, Debug class methods are not compiled and no Debug output is generated. Similarly, when Trace is defined, Trace class methods generate output to the Output window. When it is not defined, Trace class methods are not compiled and no Trace output is generated. Config is a string datatype, which corresponds to the current setting in the Configuration Manager.
statements
Required for If statement block, optional elsewhere. Visual Basic program lines or compiler directives that are compiled if the associated expression evaluates to True.
#End If
Terminates the # If statement block.
Remarks

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 Text. The Option Compare statement does not affect expressions in #If and #ElseIf statements.

Note   No single-line form of the #If, #Else, #ElseIf, and #End If directives exists; that is, no other code can appear on the same line as any of the directives.
Example

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

转载于:https://www.cnblogs.com/yangbin990/archive/2005/11/30/287446.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值