Indexers get or set values stored in an object. They are similar to .NET properties, except that they take parameters, and can be overloaded.
You can define indexers in Application objects, windows, menus, and standard and custom user objects. PowerBuilder .NET object painters provide an easy way to script "getters" and "setters" for indexers.
- Open the painter for the object in which you want to define indexers.
- From the drop-down list at the upper left of the painter, select Indexers.
An indexer signature or prototype area appears between the main item selection area and the scripting pane.
- In the second drop-down list of the main item selection area, make sure New Indexer appears.
Before an indexer is defined for the current object, New Indexer is the default selection. If indexers are already defined, the default selection is the most recent indexer viewed for the current object.
- In the drop-down list at the right of the prototype area, select:
-
get – to add a getter method.
-
set – to add a setter method.
- In the drop-down list at the left of the prototype area, select the access scope for the indexer.
Available selections are public (default), protected, and private.
- In the second drop-down list of the prototype area, select the return type for the indexer.
- Enter required information for indexer arguments:
- From the Argument Type drop-down list, select a datatype.
- In the Argument Name text box, enter an argument name.
- To add another argument, right-click in the prototype area, select Add Argument, and repeat this step as many times as required.
Note: The Script Editor shows dimmed fields for the indexer name and the argument "pass by" fields. This is because the indexer name is always "this" (meaning the current object), and you can pass indexer arguments only by value.
- Enter the processing code for the indexer getter or setter in the scripting pane.
The following code sample is from a getter script for an indexer that has a single integer argument, nIndex, and returns a string array or a simple string:
if nIndex >0 or nIndex <= names.Length then
return names[nIndex]
else
return "null"
end if
The code sample for a setter script for the same indexer looks like this:
if nIndex >0 or nIndex <= names.Length then
names[nIndex] = value
else
names[nIndex] = "no name"
end if
- To script:
- The other accessor type (getter or setter) for the current indexer, repeat this procedure from step 4, making the other accessor type selection from the one you previously made.
- A new indexer for the current object, repeat this procedure from step 3.
- Save the object.