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 Visual COBOL > 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 static constructor or instance 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

CONSTANT Keyword

Use the CONSTANT keyword to declare 01-level fields as constants.

The VALUE clause must be a literal value or a constant defined within this or another class.

For numeric fields, the VALUE clause may be an arithmetic expression. The arithmetic expression may contain parentheses, and is evaluated according to the normal rules of arithmetic expressions and according to type, so truncation may occur.

For string fields, the VALUE clause may be a string constant or a string concatenation expression containing only constants.

EVENT Keyword

The EVENT keyword is allowed only on items that are declared as delegates. It causes the creation of an event, which may be static or instance depending on the presence of the STATIC keyword.

Methods, delegates and anonymous methods, which are compatible with the underlying delegate type of the event, can be attached to the event using the ATTACH verb, and can subsequently be detached using the DETACH verb. For example:

class-id MyList using I.
working-storage section.
01 ChangeEvent type ChangeDelegate event public.
…
end class
class-id a.
 
method-id main static.
 declare names as type MyList[string] = new MyList[string]
 attach method ListChanged1 to names::ChangeEvent
 …

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

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 properties with the same name and type. In this case, by using the FOR phrase, you can supply two different implementations of the event for the two different interfaces.

01 ChangeEvent type ChangeDelegate EVENT FOR MyInterface1. 
01 ChangeEvent type ChangeDelegate EVENT FOR MyInterface2.

The FINAL Clause

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

01 ChangeEvent type ChangeDelegate EVENT FINAL.

The OVERRIDE Clause

Use the OVERRIDE clause to override an event of this name and type in an inherited class. Only virtual events in the inherited class can be overridden in this way. The OVERRIDE and the REDEFINE clauses are mutually exclusive.

 01 ChangeEvent type ChangeDelegate EVENT OVERRIDE.

The REDEFINE Clause

Use the REDEFINE clause to redefine an event of this name and type in an inherited class. Both virtual and non-virtual events in the inherited class can be redefined in this way. The OVERRIDE and the REDEFINE clauses are mutually exclusive.

01 ChangeEvent type ChangeDelegate EVENT REDEFINE.

See also the Events sample, which is available from Start > All Programs > Micro Focus Visual COBOL > 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 FOR Clause

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

01 public-field2 pic x property as "Property1" FOR MyInterface1.
01 public-field2 pic x property as "Property1" FOR MyInterface2.

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. The OVERRIDE and the REDEFINE clauses are mutually exclusive.

 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. The OVERRIDE and the REDEFINE clauses are mutually exclusive.

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 Visual COBOL > Samples, under COBOL for .NET .