Previous Page | Next Page

Example: Creating An Object-Oriented Application in SCL

Other Classes and Further Abstraction

Given the reader interface, you can now use other classes - even ones outside the data class hierarchy - as readers, as long as they support the reader interface. Using the abstract reader interface enables you to read from many different types of objects as well.

For example, consider the following class, which uses SCL lists to maintain data:

class lst supports reader;
  private list listid;
  private num nvars;
  private num nelmts;
  private num cur;

  /* Constructor */
  lst: method n:num;
       listid = makelist();
       nvars = n;
       nelmts = 0;
       cur = 1;
  endmethod;

  /* Copy method */
  cpy: method n: num return=string;
        dcl string c = "";
        if (cur <= nelmts) then do;
          c = getitemc(listid, cur);
          cur + 1;
        end;
        return c;
  endmethod;

 /* Read method */
  read: method return=num;
     if (cur > nelmts) then
         return 1;
     else
         return 0;
  endmethod;

  /* Add an element to the list */
  add: method c:string;
       nelmts + 1;
       rc = setitemc(listid, c, nelmts, 'Y');
  endmethod;

  /* Add two elements to the list */
  add: method c1:string c2:string;
        add(c1);
        add(c2);
  endmethod;

  /* Terminate the list */
  _term: method /(state='O');
      if listid then listid = dellist(listid);
      _super();
  endmethod;
endclass;

This class represents a list, and because it supports the READER interface, it can be read in the same way as the DDATA and FDATA classes.

The SCL for reading from the list is

init:
 dcl lst lstClassID;
 dcl read r;
 lstClassID = _new_ lst(2);

 /* Notice the overloaded add method */
 lstClassID.add("123", "456");
 lstClassID.add("789", "012");
 lstClassID.add("345", "678");

 /* Create a read class and loop over the data */
 r = _new_ read(lstClassID);
 r.loop();

 r._term();
 return;

The output for this program will be

123 456
789 012
345 678

Previous Page | Next Page | Top of Page