Previous Topic Next topic Print topic


Example of Using the ILSMARTLINKAGE Compiler Directive

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.

Create a managed project

First, you are going to create a project to hold the procedural program:

  1. In Visual Studio, create a managed COBOL class library project - click File > New > Project, and click Class Library in the COBOL > Managed category.
  2. Specify a name such as calclib, disable the checkbox for Create directory for solution, and click OK.

    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.

  3. Right-click the project, and click Add > New Item.
  4. Click COBOL program in the COBOL Items, specify Calc.cbl in the Name field, and click OK.

    This adds the new program to the project and opens it in the editor.

  5. Replace the code in the program with the following:
           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.

  6. In the IDE, navigate to the project properties, click the COBOL tab, and check Expose group linkage items to managed code.
  7. Click File > Save All.
  8. Click Build > Build Solution in order to compile the source program.

    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.

Write a managed front-end application in COBOL

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:

  1. In your solution, create a new managed COBOL console application project - right-click the solution in Solution Explorer, and click Add > New Project.
  2. Under Managed > COBOL, click Console Application, specify a name such as OOCalc, and then click OK.
  3. Add a project reference for this project to the original calclib project - right-click OOCalc, and click Add Reference.
  4. In the Add Reference dialog box, expand solution, click Projects, enable the checkbox in front of the Calc project, and click OK.

    This ensures that the OOCalc project will be able to access the build output files from the calclib project.

  5. Create a new managed class Class1 in the project, and add the following code that will access the group items in the procedural code:
           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).

Previous Topic Next topic Print topic