Freetutes.com

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

Declaring Compiler Constants

Besides the predefined constants, you can also define preprocessor constants for yourself to automate your preprocessing requirements.

You can declare a compiler constant in the following three ways:

Each method produces slightly different results from the other two. First, it is important to understand the mechanics of declaration for each case. After that’s clear, the discussion examines how each method behaves.

Declaring in Code

There is one more preprocessor directive to remember in addition to the #If, #Else, #ElseIf, and #End If talked about earlier: #Const. Not surprisingly, the #Const preprocessor directive is similar to the VB keyword Const. Const enables you to define a name to use in place of a constant value in your VB code, such as this:

Const MAX_LINES as Integer = 60

Then, whenever you need to use this value, you can type MAX_LINES rather than 60. This makes your code easier to understand because your code can use a meaningful name rather than a magic number. For instance,

Do While iCount <= 60

is harder to understand and more difficult to maintain than the following:

Do While iCount <= MAX_LINES

In the first case, it is not clear what makes 60 a special value; and if the value ever needs to be changed, every conditional that uses it must be changed manually. In the second case, the use of a meaningful constant name clarifies the meaning of the value; and if it ever needs to be changed, a single change to the constant definition propagates the change to all instances of its use.

#Const works in much the same way for the preprocessor. For instance:

#Const TESTING = 1

Just like a programming constant, a compiler constant must be defined only once. The syntax for compiler constants is slightly different, however, from programming constants. Although you can optionally specify a data type with a programming constant (if you don’t, the constant defaults to a Variant, or whatever the programmer has specified as a default type via the family of Def statements— for example, DefInt, DefLng, and so on), it is not possible to specify a data type for a preprocessor constant. All preprocessor constants are treated as if they are Variants of type string.

You may notice something slightly odd about this chapter’s treatment of compiler constants: Even though you generally use them as Booleans, the sample constants you will see in this chapter have all been assigned numeric values. In fact, the values True and False can be used for compiler constants too. The reason this chapter avoids doing so is that the value for True is explicitly defined as –1, which can lead to some puzzling results with the preprocessor:

#Const VexingVariable = True
#If VexingVariable = 1
MsgBox "You Won’t See Me"
#End If

The message box won’t appear because the preprocessor sees the test as –1 = 1, which it concludes is False. If you are accustomed to thinking of any non-zero value as True, this can be confusing. That is why this chapter assigns unambiguous literal values to compiler constants rather than Boolean values.

Once again, remember that the preprocessor and the compiler don’t speak the same language. (In this case, perhaps it is more appropriate to say that they don’t share the same name space.) Just as the preprocessor can’t use a variable you have declared in your code, your code can’t use compiler constants anywhere but in a preprocessor directive. This is illegal:

Dim ProgramVariable as Integer
#Const NothingButACompilerConstant = 1
If ProgramVariable = NothingButACompilerConstant Then
DoSomething()

When you try to compile code like this with Option Explicit, VB will tell you that you haven’t declared NothingButACompilerConstant. By now you have certainly noticed the pattern. So long as you don’t mix the stuff you do on lines beginning with pound signs with any of your other code, you should not have any trouble. Just keep track of those pound signs.

Declaring in the Project Properties Dialog Box

If you pull down the Project menu and choose Properties at the bottom of the menu list, the Project Properties dialog box appears. If you select the Make tab on the dialog box as shown in Figure 20.22, you will see a field for Conditional Compilation Arguments.

The Make tab of the Project Properties dialog box.
FIGURE 20.22 The Make tab of the Project Properties dialog box.

To define a preprocessor constant here, you don’t use #Const, but the syntax is otherwise the same as for doing so in code: Type a name for the constant, an equals sign, and the value you wish to assign to the constant. As the illustration shows, you can define more than one constant in the Project Properties dialog box by separating them with colons. (When you realize that the colon also acts as a line separator in VB code, this makes pretty good sense.)

Declaring in the Command Line

You can also use the VB command line to set up preprocessor constants. If you want to compile a project from outside the development environment, you can start the compilation process from a command line with the /make switch, adding the /d switch to enter compiler constants. For example:

vb.exe /make ProjectName.vbp /d VERSION=2:CLIENT="Pointy-haired boss"

You don’t need to leave a space between the /d switch and the constant, but it makes the example easier to read. Notice how the example defines multiple constants by separating them with colons.

Scope and Persistence of a Compiler Constant


  

<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved