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

Specification of properties
 property-header procedure-division-header access-modifier statement-block access-modifier statement-block

property-header

Syntax of Property-ID type-specifier access-modifier attribute-clause

Example

class-id Things.

01 _size        binary-long private.
01 ReadOnly     binary-long property with no set value 3. 
01 ReadWrite    binary-long property. 

property-id Size binary-long.
 *> Use property-value inside properties to
 *> pass the value in or out
getter.
    set property-value to _size
setter.
    if property-value < 0
        set _size to 0
    else
        set _size to property-value
    end-if
end property.

method-id main static.
    declare foo = new Things()
    add 1 to foo::Size
    display foo::Size
    display foo::ReadOnly
    set foo::ReadWrite to 22
end method.

end class.
property-id PropertyReadOnly string.
  getter.
    set property-value to field-5     
end property.

See also the Properties sample, available from Start > All Programs > Micro Focus Enterprise Developer > Samples > Visual COBOL Samples, under COBOL for .NET.

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.