VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 20

Preprocessor Directives

A preprocessor gets its name precisely because it operates on source code before the compiler processes the code. The only function of the VB preprocessor is to conditionally evaluate code to determine which parts of it the compiler should process.

The preprocessor uses just the following four conditional flow control directives:

  • #If

  • #ElseIf

  • #Else

  • #End If

Aside from the "#" prefix, the preprocessor syntax is just like that of the If, ElseIf, Else, and End If flow control directives in the Visual Basic programming language. The only difference is that If/Then/Else/End If conditional tests used in a program determine the path of execution taken by the code, whereas the #If/#Then/#Else/#End If preprocessor conditional tests determine which code will be included when a project is compiled.

Because any code that the preprocessor screens out is completely absent from the final compiled product, its presence in the source code does not influence the executable program in any way. No size or performance penalty accrues.

The formal syntax for the preprocessor directives is represented like this in the VB Help system (blocks enclosed in brackets are optional):

#If expression Then
[ #ElseIf expression-n Then
[ elseifstatements ] ]
[ #Else
[ elsestatements ] ]
#End If

At minimum, a preprocessor block must consist of #If/Then/#End If. Optionally, any number of #ElseIf blocks can be included with additional expressions to be evaluated should the expression in the main #If be False. A single #Else block can be included, but is not required. The #Else block has no expressions to evaluate because it is the default block. It is reached only if all the #If and #ElseIf expressions evaluate as False.

The sections marked statements, elseifstatements, and elsestatements represent lines of VB code that are subject to conditional compilation. After the conditional expressions are evaluated, only those lines found in the active conditional block (that is, the lines in an #If or #ElseIf block that evaluates as True, or, if no expression is True, the lines in an #Else block) will be passed to the VB compiler. Besides containing lines of VB code, it is also possible to nest additional preprocessor blocks here.

If you know how VB evaluates a programming If block, you already know how to evaluate a preprocessor #If block. A conditional expression is evaluated on the #If line. Should the conditional expression be evaluated as True (that is, any non-zero value), the set of statements immediately following the #If line will be included in the compiled form of the program, until an #End If (or, optionally, an #ElseIf or #Else) is encountered. If the conditional expression is not True, the code in the #If branch of the conditional block is excluded from the compiled form of the program.

Should the preprocessor find an #ElseIf after an #If expression evaluates as False, the preprocessor evaluates the #ElseIf expression, applying to it the same guidelines as previously described for the #If block. This process continues until the preprocessor finds an expression that evaluates to True, whereupon the code wrapped in that branch will be sent to the compiler.

When no #If or #ElseIf condition evaluates to True, those branches are excluded from the compiled form of the program. The preprocessor then tries to find an #Else branch. If one is present, any code wrapped in it will be included by default in the compiled form of the program.

Again, if you understand VB flow control with an If block, you shouldn’t have any difficulty with the logic used in the VB preprocessor. They follow the same rules, with just one exception:

A conditional expression and a single action are allowed to appear on a single line in a VB program, such as this:

If TestCondition = True Then DoSomething()

When a single action is involved, the usual closing End If is unnecessary. (Of course, if more than one action should be taken as a result of a conditional test, this form can’t be used—all the actions must appear on separate lines, and the terminating End If is required.)

With the preprocessor, however, the conditional test must be on a different line from any actions, even if there is just one action to perform. To work with the preprocessor, the preceding code would have to be written like this:

#If TestCondition = True Then
#End If

The reason for keeping preprocessor commands on separate lines from other VB actions is due to the way the preprocessor does its job. When it determines what portions of the source code to pass on to the compiler, it does more than just remove the code blocks that didn’t satisfy its conditional tests: It also strips out the tests themselves, as well as every other line that begins with a preprocessor directive. After all, the preprocessor commands are not part of your program. (Think about it—they aren’t in the Visual Basic programming language, so the VB compiler doesn’t know how to compile them.) If a programming command were to appear on the same line as a preprocessor directive—as is the case with the single-line version of a conditional test shown previously—it would also be stripped out of the program.

It may help to think of the preprocessor and the VB compiler as two separate compilers that don’t speak each other’s language. So as not to confuse them, keep the commands for one separated from the commands for the other.


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved