The following example illustrates how you can expose a procedural program to managed applications by compiling it to managed code with the ILSMARTLINKAGE Compiler directive. This helps expose the Linkage Section items and entry points in the procedural code to other managed languages by generating a class for each group item with the lower level data items being the members of the class.
In this example, you are going to use a slightly modified version of the calculator program which was used in the example of using wrapper classes.
First, you are going to create a project to hold the procedural program. You start by creating 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 op-1 pic 9(4) comp-5. 03 op-2 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 op-1 to op-2 giving the-result when sub-op subtract op-2 from op-1 giving the-result when mult-op multiply op-1 by op-2 giving the-result when div-op divide op-1 by op-2 giving the-result end-evaluate. goback. end program Calc.
This variant of the example uses hyphenated variables, op-1 and op-2, to illustrate how ILSMARTLINKAGE deals with these.
Compiling with ILSMARTLINKAGE exposes the group item in the program as a new class, and the group items as properties of that class. Other managed languages do not recognize hyphenated data items but ILSMARTLINKAGE caters for that as well - it removes the hyphens from the names of the data items and changes the case to camel case. As a result, other managed languages will see operands exposed as the class Operands, and the variables op-1 and op-2 exposed as the properties Op1 and Op2 of that class. Similarly, func-code is exposed as FuncCode.
Next, you are going to create a managed COBOL program that will access the group item entries of the procedural code now exposed as a class and its properties:
This ensures that the OOCalc project will be able to access the build output files from the calclib project.
class-id OOCalc.Class1. working-storage section. 78 add-op value '+'. 78 sub-op value '-'. 78 mult-op value 'x'. 78 div-op value '/'. method-id. main static. procedure division. declare calculation as type Class1 set calculation to new Class1 invoke calculation::Add() end method. method-id Add public. local-storage section. 01 operand type Operands. 01 CalcOO type Calc. 01 func-code type FuncCode. 01 result pic x(4) comp-5. procedure division. set func-code to new FuncCode() set operand to new Operands() set operand::Op1 to 1. set operand::Op2 to 2. set CalcOO to new Calc() set func-code::FuncCode to add-op invoke CalcOO::Calc(func-code, operand, result) display "The result is " result goback. end method. end class.
In Class1, you define the static method main which is the main entry point for your program. main defines the object calculation of type Class1, and then executes the Add method of Class1.
The code defines the method Add that uses as variables the following objects - operand of type Operands (the group item from the procedural program now exposed as a class); CalcOO of type Calc (the procedural program now seen as a class); func-code of type FuncCode (which is how func-code in the procedural program is exposed).
You then create instances of func-code, operand, and CalcOO, give values to op-1 and op-2 in the procedural program, specify that you would like to invoke the add-op operation, and invoke the procedural code using func-code, operand and result as parameters to perform the arithmetic calculation - invoke CalcOO::Calc(func-code, operand, result).