Advanced PowerBuilder

HomePrevious Lesson: Customizing Toolbars
Next Lesson: Communication Between Objects

Scoping Variables

Variables can be declared at four different places, depending on the scope you wish to associate with them. A variable's scope indicates its lifetime and the influence it can have over the rest of the application.

There are four levels of scope:
Local
Global
Shared
Instance

Local Variables

Do you know what type of variables we were using till now? Right!, Local Variables. Local variables are declared in the script itself and they are available only in the script where they were declared. This variable can't be referred in any other events or functions. For example, if you declare a variable in the Clicked event of a CommandButton, they are available only in that event. It is not available even if you try to access that variable, say, from rButtonDown event of the same object. Local variable comes into existence only when the script for the event/ function starts executing and is destroyed upon the completion of the script.

Global Variables

Did you know that we used few global variables till now and that too without even declaring them. They are SQLCA and ERROR object. These two are built-in global variables. Declare global variables by selecting Declare/Global Variables...' from the menu, when in the script painter for an object.

Global variables can be referred from anywhere in the application. They become available when the application is opened and are destroyed when the application is closed. Use Global variables only when absolutely necessary and take care while naming them.  They shouldn't conflict with other variables.

Shared Variables

Shared variables are stored in an object's definition and are available for all instances of the object. The first instance of the variable is initialized, when an object that has the shared variable is opened, and exists until the close of the application. The value of the variable persists even if the object is closed.

One example of how you could use this type of variable is when you want to count the number of times an object has been opened in an application. For each instance that is opened, the shared variable is incremented by one and the destruction of the instance has no effect on the value or on the existence of the variable itself.
If you open multiple instances of an object, the same value is used by all instances.
If the value of the variable is changed in one instance, the change will be reflected in all other instances of the object.
The value of the variable persists, even after all instances of the object are closed.

Shared variables can be declared for an application, menu, window and user objects. No matter what kind of objects the shared variables are associated with, they are declared using 'Declare/Shared Variables...' , while the appropriate object is available in the painter.

Instance Variables

Instance variables are created when an instance of the object is created and is destroyed when the object's instance is destroyed. If more than one instance of an object is opened, the value of the instance variables can be different in each case.

Instance variables can be declared for application, menu, window and user objects. The procedure for declaring instance is same as global and shared variables, i.e., by selecting 'Declare/Instance Variables...' from the menu. When declared for windows or user objects, they are available to the controls which are part of the object. For example, if you declare an instance variable at the window level, you can refer to it in the script/function of any control that is placed on that Window. .
HomePrevious Lesson: Customizing Toolbars
Next Lesson: Communication Between Objects