Advanced PowerBuilder

HomePrevious Lesson: Name Spaces
Next Lesson: Access Levels - Functions

Access Levels - Variables

As we have seen, when instances from generic classes are created, they acquire two types of variables: instance and shared variables. The instance variables are used to hold any information that is directly related to that instance of the object, while shared variables hold information that is applicable to all of the instances derived from their associated class.

Scoping of variables can be very useful. In certain circumstances, it becomes even more necessary to further refine the availability of these variables, throughout the application. You must remember that even though an instance variable holds information directly related to the current instance, it is still available for interrogation and manipulation by other objects in the application.

To solve this problem, PowerBuilder provides Access Levels. Access levels say, which object can access the specified variable. You can specify three different access levels:

Access Level

Description

Public

Accessible by all objects in an application - this is the default access level for all user defined objects. Global variables are always public.

Protected

Only accessible by that object and also by its descendant objects.

Private

Only accessible by other functions and scripts declared for the same object. Shared variables are always Private.

These user defined access levels only apply to instance variables, because other variable types have access levels that can't be changed.

By default all instance variables are public. To specify other access level to instance variables declared at the object, specify the keyword private or protected before the data type. For example:
Private Int ii_counter1, ii_counter2

When you specify an access level, all the following variables are assigned that access level until another access level declaration is encountered. In the following example, all the variables will be Private, until it encounters either Public or Protected.
Private:
Int ii_counter1
Int ii_counter2
Protected:
Int ii_counter3

With version 5.0, further restrictions (listed below) can be applied on variables.

Modifier

Description

PrivateRead

Only the defining class can read.

PrivateWrite

Only the defining class can write.

ProtectedRead

Only the defining class or direct descendants can read.

ProtectedWrite

Only the defining class or direct descendants can modify.

To support new restrictions, the syntax is modified as follows:
{Visibility}{ReadAccess}{WriteAccess}<data type><variable name>

Declaring a variable "Protected PrivateRead PrivateWrite" makes the variable visible and prevents its descendants from declaring the same variable again. PowerBuilder gives an informational message as shown below, when you try to declare it again, but won't prevent you from saving the object.
Information C0148: The identifier 'ii_wo_var1' conflicts with an existing property with this name. The new definition of 'ii_wo_var1' will take precedence and the prior value will be ignored until this version of 'ii_wo_var1' goes out of scope

If you try to read or modify the variable in the descendent scripts, you see the following message. In short, declaring the same variable in the descendent is of no use.
Error C0158: The property "ii_wo_var1" was found in class "w_parent", but insufficient rights are available to read its value.
Error C0143: This property only be modified by an event or function in it's parent class.

In some cases, like the following code, it won't prevent you from declaring and initializing it, at the same time.
// Ancestor
Protected PrivateWrite ii_wo_var1 = 100
// Descendent 
/* You will get information message, but it won't
 prevent you from declaring it. */
ii_wo_var1 = 200 
// In the script
// The MessageBox() displays 200
MessageBox( "Value in descendent", ii_wo_var1 )

The following table lists possible combinations and its validity.

Read/Write Modifier/ Visibility Modifier

Public

Protected

Private

ProtectedRead

 

X

 

ProtectedWrite

 

X

 

PrivateRead

   

X

PrivateWrite

   

X

HomePrevious Lesson: Name Spaces
Next Lesson: Access Levels - Functions