Conditional Compilation
Although debugging techniques can help you iron out problems in your code,
you may want to simply add chunks of code at compile time (if you're
currently in the debugging phase of your project) or remove the same chunks
when you release your code. Conditional compilation makes it possible to
add and remove chunks from the executable.
Conditional compilation relies on compiler directives in your code that
indicate to the compiler, as it's compiling your code, how it should use
the code. Compiler directives are #If statements that direct the compiler
as to which code it should leave in and which code it should leave out. For
example, you might wish to put debugging code in your application, but when
you create a final compile, you don't want the debugging code to be there.
Or maybe you wish to take out the code for certain features if you are
creating a demo version of your product.
To use conditional compilation, you will first need to declare a compiler
constant. You declare then using the #Const statement. Each constant is
given a unique name and assigned a value. Once these constants are
declared, you can use them within an #If… #End If block. As you'll see,
there are several ways to define conditional compilation constants.
Declaring a File-Level Compiler Constant
You can declare a compiler constant anywhere within a file in your program.
That compiler constant is then available anywhere within that file. It does
not matter whether you place the constant declaration at the top of the
file, within a defined class, or within a procedure, it will still be
available throughout the whole file. For example, the sample page
DebugForm.aspx defines the following constant at the top of its code-behind
file:
#Const DEMO = True
Using Conditional Compilation Constants
To create a compiler directive, use the #If… #End If construct. You might,
for example, want to include one block of code for demo versions of your
application and different code for retail versions, like this:
Private Sub ConditionalSample()
#If DEMO Then
lblMessage.Text = "Demo version"
#Else
lblMessage.Text = "Retail version"
#End If
End Sub
NOTE
You may be wondering why you'd use conditional compilation rather than
simple conditional statements in your code. One good reason is that you may
have large blocks of code that are different for two versions of your
application. There's no reason to load both sets of code when they're
mutually exclusive. Instead, you can make the choice as to which block you
want to include at compile time (not at runtime) using conditional
compilation.
Unlike a normal If statement, the #If statement actually removes the unused
code as it compiles. This leads to a reduced application size in memory and
prevents unnecessary or unwanted code being deployed as part of the
application. For the preceding example, the compiler will actually only see
the following code (assuming that the DEMO constant is set to True):
Private Sub ConditionalSample()
lblMessage.Text = "Demo version"
End Sub
TIP
Visual Studio .NET provides two built-in compile-time constants: DEBUG and
TRACE. By default, they're both defined for you (although you can modify
this behavior using the Project Properties dialog box). The DEBUG constant
is defined to be True when you are compiling a Debug build of your program.
(You can choose what type of build you are making by selecting Build,
Configuration Manager from the menu bar and then choosing either the Debug
or Release version. Once you set your application to build as Release, the
Debug constant is set to False.) The TRACE constant allows you to control
runtime tracing of your application.
Declaring a Global Compiler Constant
To declare a compiler constant that can be used throughout your whole
application, select the project within the Solution Explorer window,
right-click, and select Properties from the context menu. On the Property
Pages dialog box, select Configuration Properties, Build. Figure 9.25 shows
the Property Pages dialog box.
Figure 9.25. Use this property page to set compile-time constants.
In the Custom Constants text box, enter the constant name, an equal sign,
and the value to assign. (Compile-time constants follow the same naming
rules as any other Visual Basic .NET variables.)