Previous Topic Next topic Print topic


Fields

A class, valuetype and interface can define a set of static or instance fields.

static-or-instance-field

variable-field constant-field

variable-field

type-specifier access-modifier attribute-clause

constant-field

type-specifier access-modifier attribute-clause

Example

    01 dAmount  decimal.
    01 asNames string occurs 5.
    01 abFigures binary-long occurs any.
     
    78 Max_Students value 25.  *> default is public, binary-long

See also the Core sample available from Start > All Programs > Micro Focus Studio Enterprise Edition x.x > Samples, under COBOL for .NET .

INITIALIZE ONLY Keywords

The INITIALIZE ONLY phrase defines a static or instance field that can only be modified within a class constructor or constructor (respectively). For example:

    class-id a.
    01 s string static value "Shouldn't see this" initialize only.
    method-id main static.
        display s
    end method.
    method-id new static.
        set s to "Hello world"
    end method.
    end class.

INITIALIZE ONLY fields and constant fields differ as follows:

INITIALIZE ONLY field:
  • Can be either instance-level or static
  • Evaluated at run time
  • Can be initialized in declaration or by code in the constructor
Constant field (78-level):
  • Always implicitly static
  • Evaluated at compile time
  • Initialized at declaration only

EVENT Keyword

The EVENT keyword is allowed only on items that are declared as delegates.

The keyword EVENT on a delegate instance ChangeEvent creates two methods, which are named add_ and remove _ with the delegate instance name appended. These methods can be used to hook a new method into the delegate, and subsequently to remove it, respectively.

For example, the keyword EVENT in the following statement creates two methods add_ChangeEvent and remove_ChangeEvent:

       01 ChangeEvent type ChangeDelegate event public.
       ...
       delegate-id ChangeDelegate.
       procedure division using by value x1 as binary-long.
       end delegate.

See also the Events sample, which is available from Start > All Programs > Micro Focus Studio Enterprise Edition x.x > Samples, under COBOL for .NET.

PROPERTY Keyword

You can expose a field as a property using the PROPERTY keyword. By default, the property name will be the same as the field name (and in the same case). You can use the AS clause to specify a different external name.

01 public-field pic x property as "Property1".

The PROPERTY keyword generates two accessor methods to retrieve and update the field. The names of these methods are derived from the property name by prefixing it with 'get_' and 'set_' (.NET) or with 'get' and 'set' (JVM). These methods are commonly known as getter and setter methods. Languages that support properties will hide these methods from you and appear to allow you to get and set these member fields directly. In fact, the Compiler is substituting the property name with the relevant getter or setter.

The property thus defined is virtual by default, which means that it can be overridden in derived classes.

Use the following clauses to modify the characteristics of the property.

The FINAL Clause

Use the FINAL clause to make the property non-virtual, so that it cannot be overridden in a derived class.

 01 public-field pic x property as "Property1" FINAL.

The OVERRIDE Clause

Use the OVERRIDE clause to override a property of this name and type in an inherited class. Only virtual properties in the inherited class can be overridden in this way.

 01 public-field pic x property as "Property1" OVERRIDE.

The REDEFINE Clause

Use the REDEFINE clause to redefine a property of this name and type in an inherited class. Both virtual and non-virtual properties in the inherited class can be redefined in this way.

01 public-field pic x property as "Property1" REDEFINE.

Example:

The output from the following program should be:

P1 in b

P2 in a

P3 in a

M1 in b

M2 in a

M3 in a

class-id myTesterClass.
method-id main static.
   01 a type a value new b.

   display a::P1
   display a::P2
   display a::P3
   invoke a::M1
   invoke a::M2
   invoke a::M3
end method.
end class.

class-id a.

01 P1 string property value "P1 in a".
01 P2 string property value "P2 in a".
01 P3 string property final value "P3 in a".

method-id M1.
   display "M1 in a".
end method.

method-id M2.
   display "M2 in a".
end method.

method-id M3 final.
   display "M3 in a".
end method.

end class.

class-id b inherits type a.

01 P1 string property override value "P1 in b".
01 P2 string property redefine value "P2 in b".
01 P3 string property redefine value "P3 in b".

method-id M1 override.
   display "M1 in b".
end method.

method-id M2 redefine.
   display "M2 in b".
end method.

method-id M3 redefine.
   display "M3 in b".
end method.
end class.

See also the Properties sample, which is available from Start > All Programs > Micro Focus Studio Enterprise Edition x.x > Samples, under COBOL for .NET .

Previous Topic Next topic Print topic