qb45advr.hlp (Topic list)
CONST Statement Details
  QuickSCREEN      Details      Example      Contents      Index
──────────────────────────────────────────────────────────────────────────────
CONST Statement Details
 
Syntax
  CONST constantname = expression [,constantname = expression]...
 
  Argument       Description
  constantname   A name following the same rules as a BASIC variable
                 name. You may add to the name a type-declaration
                 character (%, &, !, #, or $) to indicate its type,
                 but this character is not part of the name.
 
  expression     An expression consisting of literals (such as 1.0),
                 other constants, or any of the arithmetic and
                 logical operators except exponentiation (^). You may
                 also use a single literal string such as "Error on
                 input". You cannot use string concatenation,
                 variables, user-defined functions, or intrinsic
                 functions like SIN or CHR$ in expressions assigned
                 to constants.
 
If you use a type-declaration character in the name, you may omit the
character when the name is used, as shown in the following example:
 
  CONST MAXDIM% = 250
  .
  .
  .
  DIM AccountNames$(MAXDIM)
 
If you omit the type-declaration character, the constant is given a
type based on the expression in the CONST statement. Strings always
yield a string constant. With numeric expressions, the expression is
evaluated and the constant is given the simplest type that can
represent the constant. For example, if the expression gives a result
that can be represented as an integer, the constant is given an
integer type.
 
  Note: Names of constants are not affected by DEFtype statements
        such as DEFINT. A constant's type is determined either by an
        explicit type-declaration character or by the type of the
        expression.
 
Constants must be defined before they are referenced. The following
example produces an error because the constant ONE is not defined
before it is used to define TWO (constants are defined from
left to right):
 
  CONST TWO = ONE + ONE, ONE = 1
 
Constants declared in a SUB or FUNCTION are local to the
SUB or FUNCTION. A constant declared outside a procedure
is defined throughout the module. You can use constants anywhere that
you would use an expression.
 
A common programming practice is to use a statement like the following
(recall that any nonzero value represents "true"):
 
  TRUE=-1
 
Constants offer several advantages over using variables for constant
values:
 
  ■ Constants need to be defined only once for an entire module.
  ■ Constants cannot be inadvertently changed.
  ■ In stand-alone programs, using constants produces more efficient
    code than using variables.
  ■ Constants make programs easier to modify.