An element can have scope at one of the following levels:
- Block scope – Available only within the block of code in which it is declared. This is the narrowest scope available.
- Procedure scope – Available to all code within the procedure in which it is declared.
- Module scope – Available to all code within the module, class, or trait in which it is declared. This is the widest scope available.
You specify the scope of an element when you declare it. The scope depends on the following factors:
- The level at which you declare the element – block, procedure, etc.
- The access level of the element.
Be careful when defining variables with the same name in different scopes, as the results can be unexpected. For more information, see References to Declared Elements.
Levels of scope
A block is a set of statements enclosed within a pair of statement clauses. The following is a list of such blocks.
- Do and Loop
- For and End For
- If and End If
- Select and End Select
- Try and End Try
- While and End While
If you declare a variable within a block, you can use that variable only within that block.
Choice of scope
Advantages of local variables