The following example illustrates how you can expose a procedural program to managed applications by wrapping the program in a managed COBOL wrapper class first. You then let other managed applications talk to the wrapper class using Object-Oriented (OO) syntax. The wrapper class, in its turn, talks to the procedural program.
There are different ways to write wrapper classes and this example two of them.
You are going to use a simple procedural COBOL program, a calculator, which performs the basic arithmetic operations. You will use Visual COBOL to create a managed project to hold the code of the procedural program and a managed COBOL wrapper class for the procedural code. You will also create a project for a managed COBOL front-end application that will communicate with the procedural module using the wrapper class.
First, you need to create a managed COBOL project to hold the procedural program and the wrapper classes. You are going to create a managed COBOL class library project
This creates a calclib subfolder for your project in the specified location. The project includes one COBOL class file, Class1.cbl which you can delete.
This adds the new program to the project and opens it in the editor.
program-id. Calc. data division. working-storage section. linkage section. 01 operands. 03 op1 pic 9(4) comp-5. 03 op2 pic 9(4) comp-5. 01 the-result pic 9(8) comp-5. 01 func-code pic x. 78 add-op value '+'. 78 sub-op value '-'. 78 mult-op value 'x'. 78 div-op value '/'. procedure division using by reference func-code operands the-result. evaluate func-code when add-op add op1 to op2 giving the-result when sub-op subtract op2 from op1 giving the-result when mult-op multiply op1 by op2 giving the-result when div-op divide op1 by op2 giving the-result end-evaluate. goback. end program Calc.
You are now going to create one of the wrapper classes for the procedural program:
This adds the file to the project and opens it in the editor.
class-id calclib.WrapperClass. working-storage section. method-id Add. local-storage section. 01 operands. 03 op1 pic 9(4) comp-5. 03 op2 pic 9(4) comp-5. linkage section. 01 the-result pic 9(8) comp-5. procedure division using by value p1 as binary-short p2 as binary-short returning the-result. move p1 to op1 move p2 to op2 call "calc" using '+' operands the-result goback. end method. end class.
WrapperClass defines a method, Add, which uses the same comp-5 data items which are used in the procedural code program. The method maps op1 and op2 to the variables p1 and p2 which are of the equivalent type in managed code. The method then calls the procedural program to perform the addition operation.
Other managed languages can invoke the Add method using the OO syntax in order to access the functionality of the original procedural program.
You are now going to create a slightly different wrapper class that will use a Data Transfer Object (DTO) also known as a property class. You are going to expose the data items in the original program as properties of the wrapper class in order to pass them to other managed languages.
class-id calclib.ClassDTO. working-storage section. 01 operands property all elementary. 03 op1 pic 9(4) comp-5. 03 op2 pic 9(4) comp-5. method-id Add. linkage section. 01 the-result pic 9(8) comp-5. procedure division returning the-result. call "calc" using '+' operands the-result goback. end method. end class.
The use of the property all elementary on operands means that all elementary group items are exposed as properties of ClassDTO and can be accessed by other managed programs using the OO syntax (for example, ClassDTO::op1 if accessed by other managed COBOL programs).
ClassDTO defines a similar method Add which calls the procedural program to perform the arithmetic operations.
An alternative of using property would be to set the keyword on each individual data item as follows:
01 operands. 03 op1 pic 9(4) comp-5 property as "Operand1". 03 op2 pic 9(4) comp-5 property as "Operand2".
This would expose only the individual data items as properties of the class under new names, Operand1 and Operand2.
You are now going to create a managed front-end application in COBOL that will communicate with both wrapper classes:
This ensures that the OOCalc project will be able to access the build output files from the calclib project.
program-id. Program1 as "OOCalc.Program1". data division. working-storage section. 01 calc1 type calclib.WrapperClass. 01 the-result pic 9(8) comp-5. 01 calc2 type calclib.ClassDTO. procedure division. set calc1 to new type calclib.WrapperClass set the-result to calc1::Add(2, 2) set calc2 to new calclib.ClassDTO set calc2::op1 to 5 set calc2::op2 to 4 set the-result to calc2::Add goback. end program Program1.
The program defines two objects - calc1 of type of WrapperClass and calc2 of type ClassDTO.
During the program execution, the code creates instances of calc1 and calc2. Then, the example code demonstrates how to create an instance of calc1 and invoke the Add method in WrapperClass to perform the addition operation.
Next, an instance of calc2 is created and it accesses op1 and op2 that are exposed as properties of ClassDTO to assign them values. Then, the Add method of ClassDTO is invoked to perform the addition.