Advanced PowerBuilder

HomePrevious Lesson: Encapsulation in PowerBuilder
Next Lesson: Name Spaces

Variables & Scoping

Declaring a variable in Power Script is simple. The following example declares an integer variable 'li_counter'.
Int li_counter

You can initialize a variable at declaration time as shown below:
Int li_counter = 10

All numeric variables are initialized to zero by default at declaration time, unless you specify some value as shown above. String variables are initialized to zero length, i.e., "". You can store up to 60000 characters in a string variable, however, when you declare a string variable, it won't occupy that much space; when you assign some value to the string, its length is automatically set to the length of the value specified.

When the user creates an object, it doesn't belong to a new type, but is an instance of an existing class. For example, if I create a new window, the resulting object is an instance of the window class inherited from PowerBuilder's window class. The resulting object is of type WINDOW, which has all the attributes and properties defined to that class.

Similar to traditional data type variables, you can declare PowerBuilder objects as variables. For example, the following example declares a window variable and a user object variable.
Window lw_Window1
uo_datawindow ludw_DataWindow1

When you declare a variable in an event script, it is destroyed as soon as the script completes execution. Variables declared in an event script are called 'local' variables. A variable in PowerScript can have one of the four levels of scope. When considering Object Oriented programming, it's important to keep track of the variables you have access to, in different areas of the application. The following table summarizes these variable types:

Scope

Description

Local

These variables are available only in the declared script and cannot be accessed by any other script or function. They are destroyed when the script or function completes execution.

Instance

Each instance of an object has its own set of instance variables. They are available to all object level scripts and functions, and is destroyed when the instance is destroyed.

Shared

These variables are available to all object level scripts and functions. They are shared among instances of an object and remain in memory even after the last instance of an object is destroyed. These variables are not available to its descendants.

Global

These variables are available to all scripts and functions in an application and are destroyed only when the application execution is completed.

Local variables are declared in event or function script. To declare variables with other scope, you need to display popup menu in the script painter select 'Go To > <Scope> Variables' menu option, where <Scope> is either Instance/Shared/Global and declare the variable as shown below, in the dialog box.

As soon as the script execution completes, a local variable is no longer available. You might think that a local variable would be available in the same event script for the descendent; it's not true. Even though you can execute ancestor event script, ancestor and descendent event scripts are entirely independent, i.e., events/functions don't have access to other event/function's local variables.

Declaring an instance variable is nothing but adding new attributes to the object. The default attributes available at any PowerBuilder object are nothing but instance variables, declared at the object level by Powersoft. You can change the attributes, by specifying the values as you paint the object or at run-time using scripts. For example, take the Window painter. You paint a Window in the Window painter and specify certain attributes, such as window type, color, title, associated menu and so on at design time. However, you can change the title of the window at run-time using the following code:
Window_Name.Title = "New Title"

Similarly, you can also access the declared instance variables. For example:
Window_Name.ib_RegularClose = True

The contents of instance variable of an object, in each instance, are independent. In contrast, as the name says, the shared variable is shared among instances of the same object. Even though you can't access a shared variable after closing all the instances of an object, the variable content is retained in the memory. When another instance is created, the previous value is still there. Creating instances is explained later in this chapter.
HomePrevious Lesson: Encapsulation in PowerBuilder
Next Lesson: Name Spaces