In this chapter, we will discuss how to convert a traditional legacy procedural COBOL program into an Object-Oriented (OO) COBOL program.
If you are already familiar with OO COBOL programming, you may wish to skip this chapter.
When you use the NetCOBOL development environment to create COM modules from COBOL programs, it is required that these programs be OO COBOL programs. This means that if you wish to migrate traditional procedural COBOL programs to COM, you must first restructure them as OO COBOL programs.
In some cases, this can be as simple as adding a few lines of code at the beginning and end of a COBOL program, defining it as a Class, and adding some code around the main portion of the program to create a method definition.
In other cases, you may need to or desire to break the program up into multiple method definitions for functionality reasons. In the examples in this chapter, we are going to look at what is required to take a simple file I/O module written in procedural COBOL and change it into an OO COBOL program.
The program we are going to start working with is shown in Figure 2.1.
Program-ID. FILEIO.
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.
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 Program.
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.Figure 2.1 A traditional procedural COBOL program
The program named FILEIO shown in Figure 2.1 is a simple callable program that manages the opening, reading, and closing of a text file. It expects to be called with three LINKAGE-SECTION parameters:
FILE-OPERATION - Specifies which file I/O operation is being requested. Valid values are "OPEN", "READ", and "CLOSE".
RETURN-STATUS - Returns the file status as a result of attempting the file I/O operation being requested.
RETURN-RECORD - Returns the record that was read when a successful "READ" file I/O operation takes place.
This program has a main EVALUATE loop that checks the file I/O operation being requested in the FILE-OPERATION parameter being passed in and performs one of three separate paragraphs that correspond to the “OPEN”, “READ” and “CLOSE” file I/O operations.
A WORKING-STORAGE SECTION data item named "File-Opened-Flag" maintains the current Open/Close state of the text file and is used for error checking purposes. For example, a “READ” request that comes before the file is successfully opened should raise an error.
The FILEIO program will be called by another program to first OPEN the file, then to READ all of the records and then to CLOSE the file. When the last record is read, a file status will be raised by the runtime system and the calling program will thus be able to check for this to know when the end of the file has been reached.
Figure 2.2 shows a main program named “TESTFILEIO” that will call the FILEIO subprogram.
Identification Division.
Program-ID. TESTFILEIO.
Environment Division.
Data Division.
Working-Storage Section.
01 File-Operation Pic X(5).
01 Return-Status Pic 99 Value 0.
01 Return-Record Pic X(132).
Procedure Division.
Move "OPEN" To File-Operation.
Call "FILEIO" 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
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
Return-Status
Return-Record
End-If.
Exit Program.Figure 2.2 The TESTFILEIO main program that will call the FILEIO subprogram
The TESTFILEIO program is also quite simple for the purposes of this illustration. It calls the FILEIO subprogram to first OPEN the text file. It then performs a loop calling FILEIO again to read each record and displays the returned records. Finally upon reaching the end of the file (determined by checking the file status code returned in the RETURN-STATUS parameter), it requests FILEIO to close the file.
Figure 2.3 shows the output of having executed the TESTFILEIO program that called the FILEIO subprogram reading a simple text file containing four records.

Figure 2.3 The output of executing the TESTFILEIO program
You may examine the FILEIO and TESTFILEIO programs in detail and execute them by loading the FILEIO.PRJ project.
We will now examine the various approaches to converting the FILEIO program into an Object-Oriented COBOL program. We will also make changes to the main TESTFILEIO program to allow it to utilize the newly converted FILEIO program