![]() Chapter Contents |
![]() Previous |
![]() Next |
| USECLASS |
| Category: | Object Oriented |
Syntax |
|
USECLASS
class-name;
<(method-implementation-blocks)> |
| ENDUSECLASS; |
| Details |
Method implementations inside a USECLASS block can include any SCL statements (except CLASS, USECLASS, or INTERFACE statements), or SCL functions and routines, including DECLARE to declare local variables. If you want to define a local variable that can be used across the class, you must define it as a private attribute either inside the CLASS statement or in the Class Editor. METHOD blocks can include labeled sections. However, labeled sections that are outside a method block must be re-coded as PRIVATE methods, and the LINK statements that call them must be changed to method calls. For more information, see METHOD.
_super.add();
_super();
| Examples |
Class work.a.group.class; Public Num nAttr; Public Char cAttr; m1: Method n:Num / (scl='work.a.one.scl'); m1: Method c:Char / (scl='work.a.one.scl'); m2: Method n:num / (scl='work.a.two.scl'); m3: Method Return=Num / (scl='work.a.two.scl'); m4: Method Return=Char / (scl='work.a.three.scl'); EndClass;
UseClass work.a.group.class;
m1: Method m:Num;
nAttr = m;
EndMethod;
m1: Method c:Char;
cAttr = c;
EndMethod;
EndUseClass; +
Programmer TWO creates method implementations in WORK.A.TWO.SCL as follows:
UseClass work.a.group.class;
m2: Method n:Num;
put n=;
EndMethod;
m3: Method Return=Num;
return(nAttr);
EndMethod;
EndUseClass;
Programmer THREE creates method implementations in WORK.A.THREE.SCL as follows:
UseClass work.a.group.class;
m4: Method return=Char;
Return(cAttr);
EndMethod;
EndUseClass;
USECLASS work.classes.myclass.class;
/* Method M1 without a signature */
M1: method;
...more SCL statements...
endmethod;
/* The next statements are invalid because */
/* they are not in a METHOD statement. They */
/* will produce a compiler warning message. */
DCL num n = 1; /* Invalid statement */
n=10; /* Invalid statement */
/* Method M1 has a numeric parameter. */
M1: method n: num;
/* Any SCL statement can be applied in the */
/* method implementation block. */
DCL num n;
n = n + 1;
...more SCL statements...
endmethod;
/* Method M1 has a character parameter. */
M1: method s: char;
DCL Char arr(3);
DCL num i;
DO i = 1 to dim(arr);
arr(i) = s;
END;
...more SCL statements...
endmethod;
/* The next statement is invalid because */
/* it is not in a METHOD statement. */
if s=1 then
put 'pass';
/* Other methods */
M2: method return=num;
...more SCL statements...
return(1);
endmethod;
ENDUSECLASS;
Assume that there are four class attributes defined for a class: N1 and N2 are numeric attributes, and C1 and C2 are character string attributes. Both M1 methods are class methods. One of the M1 methods takes no parameter. The other M1 method takes one numeric parameter. The private method M2 is used as a debugging routine. If the variables or methods are class attributes or class methods, short-cut syntax (see also Referencing Class Methods or Attributes) can be used to bypass the dot notations.
Import Sashelp.Fsp.Colleciton.Class;
Useclass work.classes.another.class;
_init: method / (State='O');
/* Equivalent of _super._init(); */
/* or call super(_self_,'_init'); */
_super();
/* Equivalent to _self_.n1=1 */
n1=1;
/* Local variable n2 dominates */
/* class attribute n2 */
n2=1;
/* Use SAS function ABS */
n3(1)=abs(n2);
/* Equivalent to _self_.c1='a' */
c1='a';
/* Local variable c2 dominates */
/* class attribute c2 */
c2='a';
endmethod;
m1: method;
/* Equivalent to if _self_.n1>0 then */
if n1>0 then
/* Equivalent to _self_.n1 + 1 */
n1+1;
else
/* Equivalent to */
/* _self_.m1(_self_.n1); */
m1(n1);
endmethod;
/* Method m1 with numeric parameter */
m1: method n: num;
if (n < 0) then
n = _n;
/* - Invoke M1 method _ */
m1(n);
m2(n+n);
DCL Collection col = _NEW_ Collection();
/* -- Must use the dot notation here - */
col.add(3);
endMethod;
/* - Private Debugging method _ */
m2: Private Method n:Num;
put 'Debugging ' n=;
endMethod;
endUseClass;
Import Sashelp.Classes;
Class work.a.Mywidget.class Extends textEntry_c;
m1: Public Method c: char / (scl='work.a.myClass.scl');
EndClass;Issue the SAVECLASS command to compile this program and create the
CLASS entry WORK.A.MYWIDGET.CLASS.
Edit WORK.A.MYCLASS.SCL, using USECLASS to include the method implementations.
Useclass work.a.Mywidget.class;
m1: Method c:char;
text = c; /* Equivalent to _SELF_.text = c; */
/* Text is an class attribute */
EndMethod;
EndUseClass;
Init:
Mywidget1.m1('Hello! ');
Return;
Main;
Return;Compile and TESTAF this program. You should see Mywidget with "Hello!"
in the text field. This example could also be done using only the CLASS block
(without USECLASS). Just put the method implementation directly in the CLASS
block.
Import Sashelp.Classes;
Class Mywidget Extends TextEntry_C;
m1: Method c:Char;
Text = c;
EndMethod;
EndClass;
| See Also |
![]() Chapter Contents |
![]() Previous |
![]() Next |
![]() Top of Page |
Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.