Chapter Contents

Previous

Next
SAS Component Language: Reference

USECLASS -- Level One

This section presents the USECLASS statement and is intended for those users who are unfamiliar with USECLASS. It is not required for the remainder of the tutorial.

A USECLASS statement binds methods that are implemented within it to the specified class definition. USECLASS allows a class's method implementations to reside in different entries other than the class declaration's entry. This is helpful if a class is complex enough to require several developers to write its methods.

The DDATA class will be modified to use USECLASS. Although this class is certainly not complex enough to require USECLASS, it illustrates its use.

First, rewrite the class specification, using the following code:

class ddata;

  /* Data */
  public string dname;
  public string mode;
  protected num fid;
  protected num nvars;

  /* Constructor method */
  ddata: method n: string m:string nv:num /
 (scl='sasuser.a.constr.scl');

  /* FETCH method */
  read: method return=num                  /
 (scl='sasuser.a.read.scl');

  /* GETVARC method */
  cpy: method n: num return=string         /
 (scl='sasuser.a.cpy.scl');

 /* CLOSE method */
  _term: method                            /
 (state='O', scl='sasuser.a.trm.scl');
endclass;
The method implementations are removed, and the method declaration statements are modified to indicate which SCL entry contains each method implementation. This new class specification should be compiled with the SAVECLASS command.

Next, create the method implementations in each entry. These should be compiled with the COMPILE command, not with SAVECLASS. SASUSER.A.CONSTR.SCL should contain

useclass ddata;

  /* Constructor method */
  ddata: method n: string m:string nv:num;
         fid = open(n, m);
         dname = n;
         mode = m;
         nvars = nv;
  endmethod;

enduseclass;
SASUSER.A.READ.SCL should contain
useclass ddata;

  /* FETCH method */
  read: method return=num;
       dcl num rc;
       rc = fetch(fid);
       return rc;
  endmethod;

enduseclass;
SASUSER.A.CPY.SCL should contain
useclass ddata;

  /* GETVARC method */
  cpy: method n: num return=string;
      dcl string c = "";
      if (vartype(fid, n) = 'C') then
         c = getvarc(fid, n);
      return c;
  endmethod;  

enduseclass;
SASUSER.A.TRM.SCL should contain
useclass ddata;

 /* CLOSE method */
  _term: method /(state='O');
        if (fid) then close(fid);
        _super();
  endmethod;
enduseclass;


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.