Example of Using a .NET COBOL Wrapper Class

The following example illustrates how you can expose a procedural program to .NET COBOL applications by wrapping the program in a .NET 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 demonstrates 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 .NET COBOL project to hold the code of the procedural program and a .NET COBOL wrapper class for the procedural code. You will also create a project for a .NET COBOL front-end application that will communicate with the procedural module using the wrapper class.

Create a .NET COBOL project

First, you need to create a .NET COBOL project to hold the procedural program and the wrapper classes. You are going to create a .NET COBOL class library project

  1. In Visual Studio, click File > New > Project.
  2. In the Create a new project dialog box, select COBOL from the Language drop-down list.
  3. Click Class Library (.NET Framework).
  4. Specify a name such as calclib.
  5. Uncheck Place solution and project in the same directory
  6. Click Create.

    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.

  7. Add a program to the calclib project to hold your procedural code program - right-click the project, and click Add > New Item.
  8. Click COBOL program in the COBOL Items section, specify Calc.cbl in the Name field, and click OK.

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

  9. Replace the code in the program with the following:
           program-id. Calc as "calclib.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.
    
  10. Click File > Save All.
  11. Click Build > Build Solution in order to compile the source program and to produce the program executables.

Create wrapper classes

You are now going to create one of the wrapper classes for the procedural program:

  1. Add a new .NET COBOL class to the project - right-click the project, and click Add > New Item.
  2. Click COBOL Class, type a name such as WrapperClass, and click Add.

    This adds the file to the project and opens it in the editor.

  3. Replace the code in WrapperClass with the following:
           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 .NET 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.

  4. The methods for the other three arithmetic operations in the procedural program are not included in this topic so you can write these now.

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.

  1. Add a new class, ClassDTO, to the project and paste the following code to it:
           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 .NET 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.

  2. You can now write the methods for the other arithmetic operations in the procedural program.

Write a front-end application in .NET COBOL

You are now going to create a .NET front-end application in COBOL that will communicate with both wrapper classes:

  1. In your solution, create a new .NET COBOL console application project - right-click the solution in Solution Explorer, and click Add > New Project.
  2. Select Console Application (.NET Framework), click Next, specify a name such as OOCalc, and then click Create.
  3. Add a project reference for this project to the original calclib project - right-click OOCalc, and click Add Reference.
  4. Click the Projects tab in the Add Reference dialog box, ensure the calclib project is selected, and click OK.

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

  5. In the Program1.cbl file of the project, add the .NET COBOL code that will interact with the wrapper classes:
           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.

  6. Make the oocalc project the Startup project - right-click oocalc and click Set as Startup Project.

    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.