#If...Then...#Else Directive VB.NET

Syntax

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

statements
Use: Required
One or more lines of code or compiler directives, which is executed ifexpressione valuates to True.

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

elseifstatements
Use: Optional
One or more lines of code or compiler directives, which is executed iffurther expressionevaluates to True.

elsestatements
Use: Optional
One or more lines of code or compiler directives, which are executed ifexpressionorfurther expressionevaluates 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, 0, False, or an empty string ("") returns True.

Example

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 morethan 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.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

VB.NET Topics