The Exception Object

Conditional Compilation

Conditional compilation is the process of writing code that might or might not be compiled into the application depending upon conditions set at compile time. Conditional compilation appeared in Visual Basic 4, and its original purpose was to allow for a single code base that could be compiled into either a 16-bit or 32-bit application. This was necessary because 16-bit and 32-bit API calls required different declaration statements and often returned values of different data types. Conditional compilation is no longer needed for this task, but it makes an excellent debugging aid.

Conditional compilation allows you to write lots and lots of debugging code that can easily be left out of a distribution build. Without conditional compilation, you'd have to comment or remove all of the debug code that you didn't want included in the final build.

Writing Conditionally Compiled Code

Conditional compilation code statements are defined using an #If #End If block. A conditional block behaves much like an ordinary If End If block in a normal procedure does. The difference is that conditional compilation statements are evaluated at compile time, not run time, and the expressions they evaluate are limited to conditional compiler constants. To define a conditional compiler constant, use the #Const statement like this:

#Const IncludeEnterpriseFeatures = True 

Notice that I didn't specify a data type you can't. You can only set the value of a conditional constant to an expression composed of a literal or other conditional compiler constants. Once defined, Visual Basic .NET knows to treat it as a string or a number. One nice feature of conditional constants is that they appear in an IntelliSense drop-down list when writing a compiler directive, as shown in Figure 13-5.

Figure 13-5. Conditional constants are easy to reference thanks to the IntelliSense drop downs.

graphics/f13ln05.jpg

Note

Conditional compilation constants can be referenced only when evaluating #If statements; these constants are not available to other code statements.


 

When creating conditionally compiled code, you enter your statements within the #If #End If as though the other conditional evaluation doesn't exist. For example the following code evaluates the compiler constant Include EnterpriseFeatures and compiles code accordingly:

   Private Sub fclsMain_Load(ByVal sender As System.Object, _                              ByVal As System.EventArgs) _                              Handles MyBase.Load #If IncludeEnterpriseFeatures Then        ' Code for enterprise features goes here. #Else        ' Code for standard features goes here. #End If    End Sub 

If the constant IncludeEnterpriseFeatures is True, the code for the enterprise features would compile into the build the code found in the #Else clause would be left out of the build completely. Likewise, if the constant was False, the #Else code would be compiled in the build while the #If code would be left out.

There are three ways to create a compiler constant:

  • Using the Property Pages dialog box

  • At the command line when using the command-line compiler

  • In your code using #Const as previously discussed.

Table 13-1 lists the scope of a compiler constant based upon the method used to declare it.

 

Table 13-1. Scope of Compiler Constants

Declared Using

Scope

Project Property Pages dialog box

Public to all files in the project

Command line

Public to all files passed to the command-line compiler

#Const statement in code

Private to the file in which the constant is declared

 

Setting Compiler Constants by Using the Property Pages Dialog Box

One or more conditional compilation constants can be defined in the Custom Constants text box on the Build page of the project Property Pages dialog box. (See Figure 13-6.)

Figure 13-6. Conditional compilation constants can be defined on the Build page of the project Property Pages dialog box.

graphics/f13ln06.jpg

To specify more than one conditional compilation constant, separate them with a colon like this:

EnglishVersion=-1:RegisteredCopy=-1 

Notice two of the check boxes in Figure 13-6: Define DEBUG Constant and Define TRACE Constant. Visual Basic .NET has internal compiler constants by these names. If you select a check box, the associated constant is given the value of True. If a check box isn't selected, the constant still exists but has the value of False. As I explain in the "Directives" section later in this chapter, you should always use the internal DEBUG conditional constant when creating debug code.

Setting Compiler Constants by Using the Command Line

With applications that can be distributed in many different forms (debug, extra features, and so on), it's not uncommon to find yourself frequently changing the values of conditional compilation constants using the project Property Pages dialog box. Because of this, Visual Basic .NET lets you specify conditional compilation constants using the command line. Using this technique, you can create separate desktop shortcuts for each "flavor" of your application that you need to compile.

Important

Command-line conditional compilation constants cannot be declared in the Command Line Arguments text box on the Debugging page of the project Property Pages dialog box; they must be declared on an actual command line as explained later. Any conditional compilation constants declared in the Command Line Arguments field are ignored.


 

To define conditional compilation constants on the command line, use the /d switch. Enter your conditional compilation constant declarations after the /d switch just as you would enter them on the project Property Pages dialog box. The following example shows a command line that launches the Visual Basic .NET compiler and compiles a project using conditional compilation constants:

vbc.exe /make MyProj.vbproj /d EnglishVersion=-1:RegisteredCopy=-1 

No space is required between the /d and the first constant name, but you can provide one as shown here. You can't, however, place any spaces in the constant declarations themselves (such as on either side of the equal sign).

Note

Command-line declarations override declarations entered on the project Property Pages dialog box, but they don't remove or erase the dialog-box settings. Subsequent compilations that aren't performed from the command line use the settings in the project Property Pages dialog box.



Practical Standards for Microsoft Visual Basic. NET
Practical Standards for Microsoft Visual Basic .NET (Pro-Developer)
ISBN: 0735613567
EAN: 2147483647
Year: 2005
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net