Previous Topic Next topic Print topic


Indexers

Indexers resemble properties except that their accessors take parameters. Indexers allow instances of a class or valuetype to be indexed just like arrays.

indexer-specification

indexer-header procedure-division-header access-modifier statement-block access-modifier statement-block

indexer-header

type-specifier indexer-id-signatureThe method signature enables you to specify passing parameters and returning items within the header of certain elements. You must omit the procedure division header within the method if you use this signature. access-modifier attribute-clause

Example

In the following example, a class is defined and provided with simple getter and setters as a means of assigning and retrieving values. Square brackets used to access arrays, use 0-based indexing. Round brackets use 1-based indexing.

       class-id SimpleIndexer.
       01 myArray string occurs 100.

       indexer-id string.
       procedure division using by value i as binary-long.
       getter.
           set property-value to myArray[i]
       setter.
           set myArray[i] to property-value
       end indexer.

       end class.

See also the IndexerDemo.cbl in the Indexers sample, available from Start > All Programs > Micro Focus Enterprise Developer > Samples > Visual COBOL Samples, under COBOL for JVM.

Previous Topic Next topic Print topic