Created On:  14 August 2012

Problem:

Application is a .NET Windows Forms application written in Visual COBOL.  Is it possible to share the working-storage data in the main form with other subforms without the requirement to pass the data in the linkage section?

Resolution:

Attached is an example of how the EXTERNAL clause may be used to share data between programs.

Form1 and Form2 both have defined in working-storage a data record defined as:

01 company-record external.
     05 contact    pic x(30).
     05 company pic x(30).
     05 phone      pic x(20).

Because it is defined as external in both programs it actually refers to the same data item so changes made to the data in one program are automatically reflected in the other.

There are other ways of sharing data between programs such as creating a class containing properties defined with the static attribute. Then all programs can access this property data by using the class-name::property-name syntax.

This example shows how to use external.

Form1:

class-id shareData.Form1 is partial
     inherits type System.Windows.Forms.Form.
working-storage section.
01 company-record external.
     05 contact      pic x(30).
     05 company   pic x(30).
     05 phone        pic x(20).
01 subform type shareData.Form2.
method-id NEW.
procedure division.
     invoke self::InitializeComponent
     goback.
end method.
method-id button1_Click final private.
procedure division using by value sender as object e as type System.EventArgs.
     set contact to txtContact::Text
     set company to txtCompany::Text
     set phone to txtPhone::Text
     set subform to new shareData.Form2
     invoke subform::ShowDialog
*> To redisplay any changes made to data.
     set txtContact::Text to contact
     set txtCompany::Text to company
     set txtPhone::Text to phone
end method.
end class.

Form2:

class-id shareData.Form2 is partial
     inherits type System.Windows.Forms.Form.
working-storage section.
01 company-record external.
     05 contact      pic x(30).
     05 company   pic x(30).
     05 phone        pic x(20).
method-id NEW.
procedure division.
     invoke self::InitializeComponent()
     set txtContact::Text to contact
     set txtCompany::Text to company
     set txtPhone::Text to phone
     goback.
end method.
method-id button1_Click final private.
procedure division using by value sender as object e as type System.EventArgs.
     set contact to txtContact::Text
     set company to txtCompany::Text
     set phone to txtPhone::Text
     invoke self::Close
end method.
end class.