Sample 16 demonstrates using a collection class for creating a class library.
This sample can be used as an example of class library creation, and can be used to create a class library in an actual program.
This sample covers only the basic operation. An easy-to-use class library can be rolled out by modifying and changing this sample.
Overview
A collection class is the generic name of a class that handles a set of objects-it is used to collectively handle and store many objects.
This sample covers the following classes.
Collect (Collection)
Dict (Dictionary)
List (List)
Class Layers
The following diagram shows the relationships between the class layers in Sample 16.
Note
In addition to the above classes, Sample 15 also includes the classes BinaryTree-Class, DictionaryNode-Class and ListNode-Class. These classes, which are used inside the List and Dict classes, are transparent to the collection class user, and are not explained here.
Collect Class
This is the highest collection class. All collection classes inherit this class.
Collect is an abstract class, and does not create any objects.
Since this class inherits the FJBASE class, all the methods defined in the FJBASE class can be used.
Definitions
CLASS-ID. Collect INHERITS FJBASE. ENVIRONMENT DIVISION. CONFIGURATION SECTION. REPOSITORY. CLASS FJBASE. OBJECT. PROCEDURE DIVISION. METHOD-ID. CollectionSize-Get. METHOD-ID. FirstElement-Get. METHOD-ID. NextElement-Get. METHOD-ID. PreviousElement-Get. END OBJECT. END CLASS Collect.
CollectionSize-Get method
This method investigates the number of elements in a set.
Parameter
None
Return value
PIC 9(8) BINARY
Returns the element immediately preceding the one currently pointed to. If a previous element does not exist, NULL is returned.
Dict Class
This class has the following features:
Each element has a key.
The key value is unique.
A key can be used for retrieval.
The key is used for ordering.
Since this class inherits from the Collect class, all the methods defined in Collect can be used as well.
Definitions
CLASS-ID. Dict INHERITS Collect. ENVIRONMENT DIVISION. CONFIGURATION SECTION. REPOSITORY. CLASS Collect. OBJECT. PROCEDURE DIVISION. METHOD-ID. Element-Get. METHOD-ID. Element-PutAt. METHOD-ID. FirstKey-Get. METHOD-ID. LastKey-Get. METHOD-ID. Remove-All. METHOD-ID. Remove-At. END OBJECT. END CLASS Dict.
Element-Get method
This method returns elements for a specified key.
Parameter
Key: PIC X(10)
Specifies a key value for an element to be returned.
Return value
USAGE OBJECT REFERENCE
Returns an element for a specified key if it is found, and returns NULL if it is not found.
Element-PutAt method
This method adds an element for a specified key. If an element with the same key already exists, it is replaced by the new element.
Parameters
Key: PIC X(10)
Specifies the key value of the element to be added or replaced.
Element: USAGE OBJECT REFERENCE
Specifies the element to be added or replaced.
Return value
None
FirstKey-Get method
This method determines the key value for the first element.
Parameter
None
Return value
PIC X(10)
Returns the key value for the first element. If the number of elements is 0, or if the key for the first element is a blank, a blank is returned.
LastKey-Get method
This method determines the key value for the last element.
Parameter
None
Return value
PIC X(10)
Returns the key value for the last element. If the number of elements is 0, or if the key for the last element is a blank, a blank is returned.
Remove-All method
This method deletes all elements contained in a set.
Parameter
None
Return value
None
Remove-At method
This method deletes an element for a specified key.
Parameter
Key: PIC X(10)
Specifies the key value for the element to be deleted.
Return value
None
List Class
This class has the following features:
Elements are arranged in a certain order.
Duplicate elements can be contained.
Since this class inherits from the Collect class, all the methods defined in the Collect class can be used as well.
Definitions
CLASS-ID. List INHERITS Collect. ENVIRONMENT DIVISION. CONFIGURATION SECTION. REPOSITORY. CLASS Collect. OBJECT. PROCEDURE DIVISION. METHOD-ID. Element-Get. METHOD-ID. Element-Insert. METHOD-ID. Element-PutAt. METHOD-ID. Element-PutLast. METHOD-ID. ElementNo-Get. METHOD-ID. LastElement-Get. METHOD-ID. Remove-All. METHOD-ID. Remove-At. END OBJECT. END CLASS List.
Element-Get method
This method returns the element at a specified location (index).
Parameter
Index: PIC 9(8) BINARY
Specifies the location of the element to be returned by an integer starting at 1.
Return value
USAGE OBJECT REFERENCE
Returns the specified element. If no element exists at the specified location, NULL is returned.
Element-Insert method
This method adds an element at the specified location (index).
Parameters
Index: PIC 9(8) BINARY
Specifies the location at which the element is to be added by an integer beginning with 1.
If a value greater than the number of elements plus 1 is specified, no element is added.
Element: USAGE OBJECT REFERENCE
Specifies the element to be added.
Return value
PIC 9(8) BINARY
Returns the location at which the element was added by an integer beginning with 1. If no element is added, 0 is returned.
Element-PutAt method
This method replaces the element at the specified location (index).
Parameters
Index: PIC 9(8) BINARY
Specifies the location of the element to be replaced by an integer beginning with 1. If a value greater than the number of elements is specified, no element is replaced.
Element: USAGE OBJECT REFERENCE
Specifies the element to be replaced.
Return value
Returns the location of the replaced element using an integer beginning with 1.
If no element has been replaced, 0 is returned.
Element-PutLast method
This method adds an element after the last element.
Parameter
Element: Specifies the element to be added.
Return value
None
ElementNo-Get method
This method checks the location (index) of a specified element.
Parameter
Element: Specifies the element whose location is checked.
Return value
PIC 9(8) BINARY
Returns the location of the element using an integer beginning with 1.
If the specified element is not found, 0 is returned.
If duplicate elements exist, the first found location is returned.
LastElement-Get method
This method returns the last element.
Parameter
None
Return value
USAGE OBJECT REFERENCE
Returns the last element. If the number of elements is 0, NULL is returned.
Remove-All method
This method deletes all the elements contained in a set.
Parameter
None
Return value
None
Remove-At method
This method deletes the element at the specified location (index).
Parameter
Index: PIC 9(8) BINARY
Specifies the location of the element to be deleted using an integer starting at 1.
If a value greater than the number of elements is specified, no element is deleted.
Return value
Returns the location of the deleted element using an integer beginning with 1.
If no element has been deleted, 0 is returned.
Programs and Files in Sample 16
COLLECT.COB (COBOL source program)
DICT.COB (COBOL source program)
LIST.COB (COBOL source program)
BIN_TREE.COB (COBOL source program)
D_NODE.COB (COBOL source program)
L_NODE.COB (COBOL source program)
SAMPLE16.PRJ (Project file)
SAMPLE16.CBI (Compiler option file)
SAMPLE16.TXT (Program guide)
COBOL Functions Used in Sample 16
Object-oriented programming functions
Class definition (Encapsulation)
Inheritance
Object creation
Method calling
Project management
Object-Oriented Syntax used in Sample 16
INVOKE and SET statements
Object properties
Method calling
REPOSITORY paragraphs
Class , object and method definitions
Building the Class Library
The Build function supported by the COBOL Project Manager is used to create the class library.
Start the Project Manager, and project file SAMPLE16.PRJ is opened.
Select Build from the Project menu.
When building terminates, the following files are created.
COLLECT.DLL
COLLECT.LIB
COLLECT.REP
DICT.REP
LIST.REP
Note
Some other files are also created, but they are not required when the class library is used.
Using the Class Library
When the sample class library to be used is installed in a program, the following files are required.
For Compiling or Linking
COLLECT.LIB (Import library)
COLLECT.REP (Repository library)
DICT.REP (Repository file)
LIST.REP (Repository file)
Install the above files to be used into a project that uses the class library. See Sample 18, ”Advanced Object-oriented Programming.”
For Executing
COLLECT.DLL (DLL file)