Visual Basic Concepts
Using Conditional Compilation
Conditional compilation lets you selectively compile certain parts of the program. You can include specific features of your program in different versions, such as designing an application to run on different platforms, or changing the date and currency display filters for an application distributed in several different languages.
Structuring Code for Conditional Compiling
To conditionally compile a part of your code, enclose it between #If...Then and #EndIf statements, using a Boolean constant as the branching test. To include this code segment in compiled code, set the value of the constant to –1 (True).
For example, to create French language and German language versions of the same application from the same source code, embed platform-specific code segments in #If...Then statements using the predefined constants conFrenchVersion and conGermanVersion.
#If conFrenchVersion Then ' <code specific to the French language version>. #ElseIf conGermanVersion then ' <code specific to the German language version>. #Else ' <code specific to other versions>. #End If
If the value of the conFrenchVersion constant is set to True at compile time, the conditional code for the French language version will be compiled. If the value of the conGermanVersion constant is set to True, the compiler uses the German language version.
Declaring Conditional Compilation Constants
There are three ways to set conditional compilation constants: in the Conditional Compilation Arguments field of the Make tab on the Project Properties dialog box, on a command line, or in code.
Conditional compilation constants have a special scope and cannot be accessed from standard code. How you set a conditional compilation constant may depend on the scope you want the constant to have.
|Project Properties dialog box||Public to all modules in the project|
|Command line||Public to all modules in the project|
|#Const statement in code||Private to the module in which they are declared|
Setting Constants on the Project Properties Dialog Box
Before creating the executable file, from the Project menu, choose Project Properties, click the Make tab on the Project Properties dialog box, and enter an argument, such as conFrenchVersion = –1, in the Conditional Compilation Arguments field (if you are compiling your application for the French language version). When you compile the program, this argument will satisfy the #If...Then condition, and the code between the #If...Then and #EndIf statements will be included in the compiled program.
If you have a complex #If...Then statement, containing one or more #ElseIf statements, you will need to set additional constants. You can set multiple constants by separating them with colons, as in the following example:
Setting Constants on the Command Line
If you want to start compilation from a command line, use the /d switch to enter conditional compilation constants, as shown here:
vb6.exe /make MyProj.vbp /d conFrenchVersion=–1:conANSI=0
No space is required between the /d switch and the first constant. Command-line declarations override declarations entered on the Project Properties dialog box, but do not erase them; arguments set on the Project Properties dialog box remain in effect for subsequent compilations.
*For More Information* See "#If…Then…#Else Directive" and "#Const Statement."