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

2.3 Converting the FILEIO3 OO COBOL Program into a Well-structured Object-Oriented COBOL Program

While the FILEIO3 OO COBOL program works perfectly fine, there is some cleanup we can perform to make it even more efficient and easier to maintain.

Specifically, we can remove the exit labels and exit statements from the end of each of the three methods (these were carryovers from when these methods used to be paragraphs). Additionally we can customize the three methods to use only the parameters they need.

Figure 2.8 shows the final FILEIO4 program which is the FILEIO3 program having been cleaned up.

 Identification Division.
 Class-ID. FILEIO4 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. OPEN-FILE.
 Data Division.
 Linkage Section.
 01 Return-Status Pic 99.
 Procedure Division Using Return-Status.
     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.
 End Method OPEN-FILE.

 Method-ID. CLOSE-FILE.
 Data Division.
 Linkage Section.
 01 Return-Status Pic 99.
 Procedure Division Using Return-Status.
     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.
 End Method CLOSE-FILE.

 Method-ID. READ-FILE.
 Data Division.
 Linkage Section.
 01 Return-Status Pic 99.
 01 Return-Record Pic X(132).
 Procedure Division Using Return-Status
 Return-Record.
     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.
 End Method READ-FILE.

 End Object.
 End Class FILEIO4.

Figure 2.8 The FILEIO4 program


In comparing the FILEIO4 program shown in Figure 2.8 to the FILEIO3 program shown in Figure 2.6, we note the following modifications:

  1. We have removed the exit labels and exit statements contained at the bottom of each of the three methods.

  2. We have altered the LINKAGE SECTIONS and associated PROCEDURE DIVISION USING … statements to reflect only the parameters required by the method. The OPEN-FILE and CLOSE-FILE methods do not need the RETURN-RECORD parameter because they don’t return a record. We have also removed the FILE-OPERATION parameter from all three methods because it is no longer required.

We must now modify the TESTFILEIO3 program to reflect the changes in the parameter structures of the three methods. This makes this program even shorter and less complicated. The resulting TESTFILEIO4 program is shown in Figure 2.9.

 Identification Division.
 Program-ID. TESTFILEIO4.
 Environment Division.
 Configuration Section.
 Repository.
     Class FILEIO4.
 Data Division.
 Working-Storage Section.
 01 FILEIO-PTR Object Reference.
 01 Return-Status Pic 99 Value 0.
 01 Return-Record Pic X(132).
 Procedure Division.
     Invoke FILEIO4 "NEW" Returning FILEIO-PTR.
     Invoke FILEIO-PTR "OPEN-FILE" Using Return-Status.
     If Return-Status = 0
         Perform Until Return-Status Not = 0
             Invoke FILEIO-PTR "READ-FILE" Using Return-Status
                                                 Return-Record
             If Return-Status = 0
                 Display Return-Record
             End-If
         End-Perform
         Invoke FILEIO-PTR "CLOSE-FILE" Using Return-Status
     End-If.
     Set FILEIO-PTR To Null.
     Exit Program.

Figure 2.9 The TESTFILEIO4 program


We now have a well-structured FILEIO4 OO COBOL program. You should now have some understanding of the issues involved and the process required for modifying an existing traditional procedural COBOL program into an OO COBOL program

In the next chapter, we will take the FILEIO4 program and create a Microsoft Windows COM component out of it. We will then illustrate how this component can be used in a Microsoft Visual Basic® (VB) application. Finally, we will create a simple Internet-based Active Server Page (ASP) application that uses our FILEIO4 COM component.