Absent Member.
Absent Member.

List of Class Objects

I know you can do this with invoke, list and add but can it be done with traditional cobol.  I want to create a list of class objects.  So I have a class that consists of student information

These all have properties set.

class-id TO_Students.
       Working-Storage Section.
               01  _studentname                     pic x(30)      private.
               01  _studentid                       pic x(9)       private.
               01  _studentmajor                    pic x(11)      private.
               01  _studentclassification           pic x(9)       private.
               01  _studentsexcode                  pic x(6)       private.
               01  _studentgpr                      pic 9(1)V9(4)  private usage comp-3.
               01  _majorcount                      pic 9(2)       private.
               01  _classcount                      pic 9(2)       private.
               01  _sexcount                        pic 9(2)       private.

Then in another cobol program I create an instance of this with a list.  So I have.

01  tso type List[type TO_Students].


When I create the list with these objects, I invoke::tso::Add(new TO_Students(prop StudentName...) etc.

How can this be done in a traditional cobol manner wherein I create tso, then write tso with the objects.

1 Reply
Micro Focus Expert
Micro Focus Expert

RE: List of Class Objects

I assume you're using managed COBOL - that is, COBOL for .NET or JVM - and not native OO COBOL.

I'll also assume you're using a relatively recent version of Visual COBOL, as you've mysteriously neglected to supply this key piece of information.

Managed COBOL has portable syntax for working with list containers. See the product documentation:




So, for example:

01 tso list[type TO_Students].

method-id new.
create tso
end method new.

method-id AddStudent(studentName as string, ...).
declare student = new TO_Student(studentName, ...)
sync on tso
write tso from student
end method AddStudent.

Out of curiosity, why are you using pic x(n) rather than managed types such as string in your TO_Student class?

The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.