Properties

Properties are members with associated types. They are similar to fields but they have accessors that provide the means to read and write values.

property-specification

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

property-header

type-specifier access-modifier attribute-clause

Example

class-id MyClass.
working-storage section.
01 volume binary-long private.
property-id Volume binary-long.   
  getter.
    set property-value to volume
  setter.
    if property-value < 0
      set volume to 0
    else
      set volume to property-value
    end-if
end property.
end class.
property-id PropertyReadOnly string.
  getter.
    set property-value to field-5     
end property.

See also the Properties sample, available from $COBDIR/demo.

Further Information

A property specified using PROPERTY-ID must contain a get or set accessor (or both), using the GETTER and SETTER keywords respectively. The Compiler then generates get_ and set_ methods accordingly.

There are two ways of exposing a field as a property within a COBOL program:

  • The simplest way is to specify the keyword PROPERTY on any data declaration within the program. This automatically sets up two accessor methods for getting and setting the value of the data item. There are optional phrases that follow the PROPERTY keyword that you can use to suppress either the get or the set accessor. You can also use the AS clause to change the external name.
  • The alternative way is to specify a PROPERTY-ID with the GETTER or SETTER phrases to be executed whenever the property is accessed. This mechanism, though slightly more complicated than the PROPERTY keyword, gives you complete control over the statements that are executed when the property is accessed. See PROPERTY Keyword

The FOR Clause

Use the FOR clause as an explicit interface implementation, which is particularly useful when the containing class implements two different interfaces, and these two interfaces have PROPERTY-IDs with the same name and type. In this case, by using the FOR phrase, you can supply two different implementations of the PROPERTY-IDs for the two different interfaces.

property-id PropertyReadOnly string FOR MyInterface1.
  getter.
    set property-value to field-1     
end property.
property-id PropertyReadOnly string FOR MyInterface2.
  getter.
    set property-value to field-2     
end property.