The general syntax for declaring PowerScript variables (see “Syntax of a variable declaration”) showed that you can specify access keywords in a declaration for an instance variable. This section describes those keywords.
When you specify an access right for a variable, you are controlling the visibility of the variable or its visibility access. Access determines which scripts recognize the variable’s name.
For a specified access right, you can control operational access with modifier keywords. The modifiers specify which scripts can read the variable’s value and which scripts can change it.
{ access-right } { readaccess } { writeaccess } datatype variablename
The following table describes the parameters you can use to specify access rights for instance variables.
Parameter |
Description |
---|---|
access-right (optional) |
A keyword specifying where the variable’s name will be recognized. Values are:
|
readaccess (optional) |
A keyword restricting the ability of scripts to read the variable’s value. Values are:
When access-right is PUBLIC, you can specify either keyword. When access-right is PROTECTED, you can specify only PRIVATEREAD. You cannot specify a modifier for PRIVATE access, because PRIVATE is already fully restricted. If readaccess is omitted, any script can read the variable. |
writeaccess (optional) |
A keyword restricting the ability of scripts to change the variable’s value. Values are:
When access-right is PUBLIC, you can specify either keyword. When access-right is PROTECTED, you can specify only PRIVATEWRITE. You cannot specify a modifier for PRIVATE access, because PRIVATE is already fully restricted. If writeaccess is omitted, any script can change the variable. |
datatype |
A valid datatype. See “Syntax of a variable declaration”. |
variablename |
A valid identifier. See “Syntax of a variable declaration”. |
Access modifiers give you more control over which objects have access to a particular object’s variables. A typical use is to declare a public variable but only allow the owner object to modify it:
public protectedwrite integer ii_count
You can also group declarations that have the same access by specifying the access-right keyword as a label (see “Another format for access-right keywords”).
When you look at exported object syntax, you might see the access modifiers SYSTEMREAD and SYSTEMWRITE. Only PowerBuilder can access variables with these modifiers. You cannot refer to variables with these modifiers in your scripts and functions and you cannot use these modifiers in your own definitions.
To declare these variables, select Declare>Instance Variables in the appropriate painter.
These declarations use access keywords to control the scripts that have access to the variables:
private integer ii_a, ii_n
public integer ii_Subtotal
protected integer ii_WinCount
This protected variable can only be changed by scripts of the owner object; descendants of the owner can read it:
protected privatewrite string is_label
These declarations have public access (the default) but can only be changed by scripts in the object itself:
privatewrite real ir_accum, ir_current_data
This declaration defines an integer that only the owner objects can write or read but whose name is reserved at the public level:
public privateread privatewrite integer ii_reserved
Private variable not recognized outside its object Suppose you have defined a window w_emp with a private integer variable ii_int:
private integer ii_int
In a script you declare an instance of the window called w_myemp. If you refer to the private variable ii_int, you get a compiler warning that the variable is not defined (because the variable is private and is not recognized in scripts outside the window itself):
w_emp w_myemp
w_myemp.ii_int = 1 // Variable not defined
Public variable with restricted access Suppose you have defined a window w_emp with a public integer variable ii_int with write access restricted to private:
public privatewrite integer ii_int
If you write the same script as above, the compiler warning will say that you cannot write to the variable (the name is recognized because it is public, but write access is not allowed):
w_emp w_myemp
w_myemp.ii_int = 1 // Cannot write to variable