next up previous contents index
Next: Exceptions Up: Programsunits, blocks Previous: Blocks

Scope

Identifiers are valid from the point of their declaration until the end of the block in which the declaration occurred. The range where the identifier is known is the scope of the identifier. The exact scope of an identifier depends on the way it was defined.

Block scope

The scope of a variable declared in the declaration part of a block, is valid from the point of declaration until the end of the block.

If a block contains a second block, in which the identfier is redeclared, then inside this block, the second declaration will be valid. Upon leaving the inner block, the first declaration is valid again.

Consider the following example:
listing4669
In this example, inside the procedure, X denotes an integer variable. It has it's own storage space, independent of the variable X outside the procedure.

Record scope

The field identifiers inside a record definition are valid in the following places:

  1. to the end of the record definition.
  2. field designators of a variable of the given record type.
  3. identifiers inside a With statement that operates on a variable of the given record type.

Class scope

A component identifier is valid in the following places:

  1. From the point of declaration to the end of the class definition.
  2. In all descendent types of this class.
  3. In all method declaration blocks of this class and descendent classes.
  4. In a with statement that operators on a variable of the given class's definition.

Note that method designators are also considered identifiers.

Unit scope

All identifiers in the interface part of a unit are valid from the point of declaration, until the end of the unit. Furthermore, the identifiers are known in programs or units that have the unit in their uses clause. Identifiers from indirectly dependent units are not available.

Identifiers declared in the implementation part of a unit are valid from the point of declaration to the end of the unit.

The system unit is automatically used in all units and programs. It's identifiers are therefore always known, in each program or unit you make.

The rules of unit scope implie that you can redefine an identifier of a unit. To have access to an identifier of another unit that was redeclared in the current unit, precede it with that other units name, as in the following example:



This is especially useful if you redeclare the system unit's identifiers.


next up previous contents index
Next: Exceptions Up: Programsunits, blocks Previous: Blocks

Michael Van Canneyt
Fri Sep 25 09:15:40 MEST 1998