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

Understanding Conditional Compilation

Ordinarily, the VB compiler reads all the source code in a project and translates it into either P-Code or native code that can be used by a computer. It doesn’t omit a single line of executable code (naturally, comments aren’t compiled), so every instruction that was in the source code also winds up in the compiled EXE, DLL, or custom control.

With conditional compilation, this no longer is the case. Using conditional compilation, it is possible for the programmer to tell the compiler whether to skip or include a portion of code, or to compile one section rather than another. (Reasons for doing these things are discussed later in the section titled "Applications and Styles.") This means that there may be code in the source files that does not get compiled into the EXE for a project.

To understand how this works, it may help to think about the compilation process as consisting of two phases. During the first phase, the source code is scanned to determine which parts of the source are actually supposed to be translated into machine code. The code that fails the test is ignored; the code that passes is written to temporary storage for use in phase two. During the second phase, the code in the temporary storage is compiled into an executable program file.

Because the determination of which code is included and which code is omitted occurs before the compiler actually processes the code, this phase is often called preprocessing. Although it was not introduced into VB until version 4.0, other languages have had access to a preprocessor for a long time.

The C and C++ preprocessor, for instance, makes it possible for the contents of one file to be inserted into another (the #include directive), to expand a token into another series of characters as a macro (the #define directive), and even to change the rules of the language temporarily (the #pragma directive). Remember that all these actions occur before the code is presented to the compiler. The compiler never sees the original state of your source code; it only operates on the code that has passed the conditional tests put in place by the programmer.

The Visual Basic preprocessor is not as powerful as that found in C or C++—VB can’t do the tricks mentioned in the preceding paragraph (not yet, anyway)—but it is still quite useful. The following page takes a look at VB’s preprocessor directives.

  1. Preprocessor Directives

  2. Types of Expressions

  3. Compiler Constants

    Predefined Compiler Constants

    Declaring Compiler Constants
    Declaring in Code
    Declaring in the Project Properties Dialog Box
    Declaring in the Command Line

    Scope and Persistence

  4. Applications and Style

NOTE - Simplifying the Discussion of Conditional Compilation: VB can produce custom controls, DLLs, or EXEs, consisting of either P-Code or native code. Rather than unnecessarily complicating the explanation of the compilation process by referring to all options every time, the rest of this chapter assumes that the programmer is compiling an EXE to native code. None of the conditional compilation concepts change if you change your project options, but the rest of the explanations in this chapter should be more readable if the discussion addresses just one case.


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved