Variable Scope and Lifetime

Variables declared within class modules exhibit the same scope and lifetime as those declared within standard modules. For example, Private module-level variables are only available to procedures within the same module, and are destroyed when the class instance is destroyed. Public module-level variables are visible to any code that has access to the class instance.

Class variables declared at procedure level remain accessible only to code within that procedure and are destroyed when the procedure exits; unless of course the variable is declared using the Static keyword. In such a case, the variable is destroyed along with the module-level variables when the object is destroyed.

Although the variables used to hold pointers to objects obey the normal scope and lifetime rules as described above, they demand special consideration, as you will soon see.

To demonstrate how variable scope and lifetime works, create the following two class modules.

0 0

Post a comment