Access for instance variables

Description

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.

Syntax

{ access-right } { readaccess } { writeaccess } datatype variablename

The following table describes the parameters you can use to specify access rights for instance variables.

Table 3-4: Instance variable declaration parameters for access rights

Parameter

Description

access-right (optional)

A keyword specifying where the variable’s name will be recognized. Values are:

  • PUBLIC – (Default) Any script in the application can refer to the variable. In another object’s script, you use dot notation to qualify the variable name and identify the object it belongs to.

  • PROTECTED – Scripts for the object for which the variable is declared and its descendants can refer to the variable.

  • PRIVATE – Scripts for the object for which the variable is declared can refer to the variable. You cannot refer to the variable in descendants of the object.

readaccess (optional)

A keyword restricting the ability of scripts to read the variable’s value. Values are:

  • PROTECTEDREAD – Only scripts for the object and its descendants can read the variable.

  • PRIVATEREAD – Only scripts for the object can read the variable.

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:

  • PROTECTEDWRITE – Only scripts for the object and its descendants can change the variable.

  • PRIVATEWRITE – Only scripts for the object can change the variable.

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”.

Usage

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.

Examples

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