The scope and persistence of a compiler constant depends on where it is declared.
If the #Const directive is used to define a compiler
constant in code, that constant is Private to the module in which it is defined.
This means that if you want to use the same constant in multiple modules, it must
be defined in each module; you can’t use #Const to
create Public compiler constants. Naturally, compiler constants defined in code
persist between sessions. That is, they won’t go away unless you explicitly
remove them from your source files.
If you need to use a Public compiler constant, you can define it either in
the Project Properties dialog box or on the command line. Constants defined in
either way are Public to all modules in a project. If you need to use the same
constant throughout an entire project, this is obviously more convenient than
manually entering it into every module and form.
Why are there two ways to define Public compiler constants? The scope is the
same using either method, but the persistence differs. If you use the Project
Properties dialog box to define a constant, it is saved with your project. If
you close the project, any compiler constants you have defined as a property of
the project will still be there the next time you open the project.
When you specify a compiler constant on the command line, it applies only to
the instance of the project that you are running at that very moment. The primary
reason for this is to enable you to use a different value for a Public compiler
constant that you have already defined in the Project Properties dialog box. The
value you use on the command line temporarily overrides the stored value, but
doesn’t erase it. Consider that a Public compiler constant is defined under
Project Properties such that USER="Wally", and
that the following is entered on the command line:
vb.exe /make ProjectName.vbp /d USER="Alice"
Any conditional compiler tests will substitute the value
"Alice" for the USER constant during
the current debug session. The next time the project is run, however,
USER will still be defined as "Wally" (unless
it is overridden on the command line again, of course). Values set on the command
line don’t persist between sessions.
What happens if you use the same name for a constant both in your code and
in the Project Properties dialog box? The same thing that happens when a local
variable has the same name as a global variable: The local variable overrides
the global variable. The value of the compiler constant set in your code will
override the Public constant set in the dialog box.
If BUDDYLIKESSOCKS = 0 is specified in the dialog
box, for example, and #Const BUDDYLIKESSOCKS = 1 is
specified in a code module, the value of BUDDYLIKESSOCKS will
be 1 in the code module and 0 everywhere
else. This enables you to override Public compiler constants on a module-by-module