Top
NetCOBOL V11.0 Getting Started with COM Components
FUJITSU Software

2.1 Converting the FILEIO Program into a Simple Object-Oriented COBOL Program

Converting the FILEIO program shown in Figure 2.1 requires some knowledge of OO COBOL, but is also a reasonably straightforward process.

The project file named FILEIO2.PRJ contains the programs we will refer to in the following text. You may use it to examine and execute the application as converted in this section.

The main TESTFILEIO program will also require a couple of modifications in order to call the OO COBOL version of the FILEIO program that will be named FILEIO2. In this first example, however, TESTFILEIO will not be converted into an OO COBOL program itself. Instead, it will be OO enabled to call the FILEIO2 OO COBOL program.

This implies that you can create applications that use both traditional procedural COBOL programs and OO COBOL programs together. A traditional procedural COBOL program requires a few minor modifications in order to call an OO COBOL program and must use the INVOKE statement instead of the traditional CALL statement in order to do so.

An OO COBOL program can call a traditional procedural COBOL program using the standard CALL statement. A traditional procedural COBOL program does not require any modification in order to be called by an OO COBOL program. Modifications to a traditional procedural COBOL program are only required when such a program wishes to call an OO COBOL program.

Figure 2.4 shows the results of converting the FILEIO COBOL program into a simple object-oriented COBOL program named FILEIO2.

 Identification Division.
 Class-ID. FILEIO2 Inherits FJBASE.
 Environment Division.
 Configuration Section.
 Repository.
     Class FJBASE.

 Object.
 Environment Division.
 Input-Output Section.
 File-Control.
     Select Infile Assign To "test.txt"
                   Organization is Line Sequential
                   File Status is Infile-Status.
 Data Division.
 File Section.
 FD Infile.
 01 Infile-Record Pic X(132).
 Working-Storage Section.
 01 Infile-Status Pic 99.
 01 File-Opened-Flag Pic 9 Value 0.
 Procedure Division.

 Method-ID. CALL_FILEIO2.
 Data Division.
 Linkage Section.
 01 File-Operation Pic X(5).
 01 Return-Status Pic 99.
 01 Return-Record Pic X(132).
 Procedure Division Using File-Operation
                          Return-Status
                          Return-Record.
     Evaluate File-Operation
         When "OPEN" Perform Open-File
         When "CLOSE" Perform Close-File
         When "READ" Perform Read-File
     End-Evaluate.
     Exit Method.

 Open-File.
     If File-Opened-Flag Not = 0
         *> File is already open, so return error
         Move 99 To Return-Status
     Else
         Open Input Infile
         Move Infile-Status To Return-Status
         If Infile-Status = 0
             Move 1 To File-Opened-Flag
         End-If
     End-If.
 Open-File-Exit.
     Exit.

 Close-File.
     If File-Opened-Flag = 0
         *> File is not open, so return error
         Move 88 To Return-Status
     Else
         Close Infile
         Move Infile-Status To Return-Status
         Move 0 To File-Opened-Flag
     End-If.
 Close-File-Exit.
     Exit.

 Read-File.
     If File-Opened-Flag = 0
         *> File is not open, so return error
         Move 88 To Return-Status
     Else
         Read Infile
         Move Infile-Status to Return-Status
         Move Infile-Record to Return-Record
     End-If.
 Read-File-Exit.
     Exit.
 End Method CALL_FILEIO2.

 End Object.
 End Class FILEIO2.

Figure 2.4 The FILEIO program converted to an OO COBOL program and renamed FILEIO2


If you compare the original FILEIO program shown in Figure 2.1 to the OO COBOL version of it named FILEIO2 shown in Figure 2.4, you will notice the following changes:

  1. The Program-ID statement has been changed to a Class-ID statement and reflects the name change (FILEIO2). It additionally contains the phrase: Inherits FJBASE (Refer to the topic entitled “Inheritance” in Chapter One for an explanation).

  2. A CONFIGURATION SECTION has been added to the ENVIRONMENT DIVISION.

  3. A Repository statement has been added to the newly inserted CONFIGURATION SECTION. The Repository is use to list any outside classes this class may refer to.

  4. A Class reference to FJBASE has been added to the newly inserted Repository.

  5. An Object statement has been added after the Repository. This defines the start of the actual object or program that will be instantiated when the FJBASE Class’ “NEW” method is invoked.

  6. An ENVIRONMENT DIVISION has been added for the Object. Note that the original ENVIRONMENT DIVISION from FILEIO has been left intact after this point. Also notice that the DATA DIVISION has been left intact as well, following this point, with the exception of the LINKAGE SECTION, which has been moved down into a method.

  7. We’ve added a PROCEDURE DIVISION statement for the object itself, and we have inserted after it a Method-ID statement and named the new method “CALL_FILEIO2”. Every class must have a least one method, and you can think of it in this example as being similar to an Entry Point. A Class can have multiple methods and each must have a unique name and may be invoked (called). Methods look very much like individual nested COBOL programs.

  8. We now define a new DATA DIVISION for this specific method, and the original LINKAGE SECTION from FILEIO has been placed here.

  9. From this point forth, we have the identical PROCEDURE DIVISION USING … statement and all of the original FILEIO program’s procedural logic, with the exception that “Exit Program” has been changed to Exit Method”.

  10. Finally, we’ve added three statements at the bottom of the program to close it off properly:

    1. End Method CALL_FILEIO2. - Ends the method definition.

    2. End Object. - Ends the object definition.

    3. End Class FILEIO2. - Ends the Class (Program) definition.

This is all that is needed to convert the traditional procedural COBOL program FILEIO to the OO COBOL program FILEIO2.

It’s important to realize that the actual procedural code remained largely intact and did not require changes, with the lone exception of Exit Program to exit Method.

Let’s look now at the changes required to the main TESTFILEIO program shown in Figure 2.2 to allow it to call our newly created FILEIO2 OO COBOL program.

Figure 2.5 shows the modified TESTFILEIO program that has been named TESTFILEIO2.

 Identification Division.
 Program-ID. TESTFILEIO2.
 Environment Division.
 Configuration Section.
 Repository.
     Class FILEIO2.
 Data Division.
 Working-Storage Section.
 01 FILEIO-PTR Object Reference.
 01 File-Operation Pic X(5).
 01 Return-Status Pic 99 Value 0.
 01 Return-Record Pic X(132).
 Procedure Division.
     Invoke FILEIO2 "NEW" Returning FILEIO-PTR.
     Move "OPEN" To File-Operation.
     *> Call "FILEIO" Using File-Operation
     Invoke FILEIO-PTR "CALL_FILEIO2" Using File-Operation
                                            Return-Status
                                            Return-Record.
     If Return-Status = 0
         Move "READ" To File-Operation
         Perform Until Return-Status Not = 0
             *> Call "FILEIO" Using File-Operation
             Invoke FILEIO-PTR "CALL_FILEIO2" Using File-Operation
                                                    Return-Status
                                                    Return-Record
             If Return-Status = 0
                 Display Return-Record
             End-If
         End-Perform
         Move "CLOSE" To File-Operation
         *> Call "FILEIO" Using File-Operation
         Invoke FILEIO-PTR "CALL_FILEIO2" Using File-Operation
                                                Return-Status
                                                Return-Record
     End-If.
     Set FILEIO-PTR To Null.
     Exit Program.

Figure 2.5 The modified TESTFILEIO2 program


If you compare the original TESTFILEIO program shown in Figure 2.2 to the modified version named TESTFILEIO2 shown in Figure 2.5, you will note the following modifications:

  1. A CONFIGURATION SECTION has been added in the ENVIRONMENT DIVISION.

  2. A Repository has been added to the newly inserted CONFIGURATION SECTION.

  3. A Class reference to the FILEIO2 class (program) we just modified has been added to the Repository.

  4. In the WORKING-STORAGE SECTION, we’ve added a new data item which will hold the pointer to the FILEIO object we’ll create a runtime. This data item is named:

     01 FILEIO-PTR Object Reference.
  5. In the PROCEDURE DIVISION, the following Invoke statement has been added to create a new instance of the FILEIO2 class (program) and return the pointer to such in the FILEIO-PTR data item:

     Invoke FILEIO2 "NEW" Returning FILEIO-PTR.
  6. The previous CALL statements to the FILEIO program have been commented out and replaced with:

     Invoke FILEIO-PTR "CALL_FILEIO2" Using File-Operation Return-Status Return-Record.

    Note that the syntax of the Invoke statement makes use of the identical USING statement and the same parameter list.

  7. 7. Just before the EXIT PROGRAM statement, we’ve added a statement to release the class object (delete it) from memory:

     Set FILEIO-PTR To Null.

These are the only changes required to the original TESTFILEIO program to allow it to call the new FILEIO2 OO COBOL program. If you execute this application (contained in the FILEIO2.PRJ project), you will see the same results as having executed the original FILEIO.PRJ. These results are shown in Figure 2.3. We have now made the minimal required changes to the FILEIO program to turn it into an OO COBOL program.