Previous Topic Next topic Print topic


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 Start > All Programs > Micro Focus Studio Enterprise Edition x.x > 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
Previous Topic Next topic Print topic