Chapter Contents |
Previous |
Next |
METHOD |
Category: | Modular Programming and Object Oriented |
Syntax |
method-name-label:<method-access-scope > METHOD <argument-list> <OPTIONAL=argument-list> <ARGLIST=arg-list-id | REST=rest-list-id><RETURN=data-type>; |
PUBLIC
scope. Method-access-scope is valid only for METHOD statements in a CLASS or USECLASS
block.
PUBLIC
PRIVATE
PROTECTED
var-list:<INPUT|UPDATE|OUTPUT>:data-type(length)
INPUT
|
I
UPDATE
| U
OUTPUT
|
O
:
delimiter. The delimiter is optional
for unnamed data types (for example, $).
CHAR
<(
length)
>Note: You cannot specify length
for the CHAR
data-type
within the RETURN option.
LIST
NUM
OBJECT
This type causes the SCL compiler to generate extra conversion instructions. Consequently, you should use it only when necessary so as to achieve optimal run-time performance.
If the entry type is not specified and a class with that name does not exist, the default entry type of INTERFACE is assumed.
:
delimiter is optional for variables that are declared with unnamed data types
(for example, $), but it is required for variables that are assigned named
data types. The following example shows a variety of data type declarations
including the reference arrays using * as the dimensions:
mymethod: method char1 : Char(20) char2 : Char(10) char3 :input :char(50) charArr(*):u:char /* a reference array */ num1 : Num num2 : Num num3 : num numArr(*):num /* a reference array */ myList :list myObj :object myCol :sashelp.fsp.Collection.class ;
Type: Character
Type: Character
Type: List
Type: List
Details |
The METHOD statement enables you to create method blocks and methods for SAS/AF classes. A method block is a feature for defining methods or for making a frequently used routine available to other programs. Methods define the actions for a class. A method block starts with the METHOD labels and ends with an ENDMETHOD statement. Only SCL entries can contain method blocks. Each method block contains additional SCL statements.
RETURN=data-type enables you to return a value to the calling method. An error condition is produced if data-type is not the same as the type of data to be returned to the calling program. Use a RETURN statement in the method to specify the value to return.
In SCL CLASS statement block or USECLASS statement block, each METHOD statement starts a new local variable scope just like an SCL DO/END block. Parameters with the same name but with the different types can be used across different method statements.
The METHOD statement receives parameters from the calling routine. When there are no optional arguments in the METHOD statement, a strict correspondence is required between the parameters that are passed by the calling routine and the arguments for the METHOD statement. The arguments and parameters must agree in number, data type, and relative position. If the calling program passes an incorrect number of parameters or a parameter of an incorrect type, SCL stops executing the program. The argument-parameter correspondence is less restrictive when you use the options OPTIONAL=, ARGLIST=, and REST= in the METHOD statement:
Other SCL programs call a method block by specifying its label in a dot notation statement or in a METHOD, APPLY, SUPER, SUPAPPLY, or SEND routine or function. Execution of the method block starts at the METHOD statement and ends with the ENDMETHOD statement. After a method block is executed, control returns either to the calling program statement or to the command line. A method block can be tested individually by invoking a TESTAF command with the label=method-name option with the SCL debugger. For example, the following statement tests the COMBINE method:
testaf label=combine
All variables that are declared using the DECLARE statement in a method block
are local to that method. You cannot use a GOTO statement to jump into a method
block in the current entry. All the method parameters are also local to that
method if method blocks are written inside a CLASS statement block or a USECLASS
statement block.
The METHOD statement can receive parameter values for variables that are declared as UPDATE or INPUT. By default, all parameters declared in a METHOD statement are UPDATE parameters.
The parameter-receiving mechanism for the METHOD statement is very similar to that mechanism for the ENTRY statement. The METHOD statement receives parameters from the third argument of the calling METHOD routine. The calling METHOD routine must agree with the corresponding METHOD statement in the following ways (unless OPTIONAL=, ARGLIST=, or REST= are specified):
Otherwise, SCL stops executing the calling METHOD routine
and prints an error message.
The METHOD statement can return values to parameters from variables that are declared as UPDATE or OUTPUT. A called method block can modify any argument it receives. However, it cannot return new values to calling routine parameters that are numeric literals, character literals, or expressions. By default, values for variables are returned to the calling routine. If you want a called method block to receive values but not to return values to its calling routine, declare the variables as INPUT. If you want variables in the method to only return values, then declare the method's variables as OUTPUT.
For more information, see What Happens When Attribute Values Are Set or Queried.
A METHOD statement can return a value to the calling routine when the METHOD statement uses the RETURN= option to declare the data type of the returned value. A RETURN statement in the method specifies either the variable or expression that contains the value or the literal value to return.
Examples |
Method M1 contains a variety of argument specifications.
IMPORT work.myclass.collection.class; Class Example1; M1: PUBLIC METHOD /* usenum is UPDATE (default) numeric */ usenum :NUM /* usechar is UPDATE (default) character */ usechar :CHAR /* mylist is UPDATE (default) list */ mylist :LIST /* myobject is UPDATE (default) object */ myobject :OBJECT /* mycoll is UPDATE (default) collection */ mycoll :COLLECTION /* innum is INPUT numeric */ innum :INPUT :NUM /* state is OUTPUT character */ state :OUTPUT :CHAR /* namelist is UPDATE list */ namelist :UPDATE :LIST /* outputobj is OUTPUT object */ outputobj :OUTPUT :OBJECT /* amountin is INPUT numeric */ amountin :I :NUM /* c3 is OUTPUT character */ c3 :O :CHAR /* l3 is UPDATE list */ l3 :U :LIST /* numarr is a numeric UPDATE array */ numarr(5) : NUM /* states is a character reference array */ states(*) : CHAR /* return a numeric value */ RETURN=NUM; ...SCL statements that define the method... RETURN(0); ENDMETHOD; EndClass;
Define an ADD method to add the numbers stored in N1 and N2, and return the sum in the variable TOTAL:
Class Example2; add: public method n1:num n2:num return=num; total=n1+n2; return(total); endmethod; EndClass;
The following Sort class contains two overloaded methods that are named SORT. Each method contains an array parameter that is a reference array. The size of the reference array will be determined at run time based on the associated array parameters in the calling methods.
Class Sort; /* Generic sort routine for any size of */ /* 1-dimensional numeric array */ sort: method narr(*):Num; /* Find dimensions from the calling program */ DCL Num temp; DCL Num size = dim(narr); /* --- Bubble Sort --- */ do i = 1 to size - 1; do j = i+1 to size; if narr(i) > narr(j) then do; temp = narr(i); narr(i) = narr(j); narr(j) = temp; end; end; end; /* Array narr is now sorted in ascending order */ endmethod; /* Generic sort routine for any size of */ /* 1-dimensional character array */ sort: method carr(*):Char; /* Find dimensions from the calling program */ DCL Char tempc; DCL Num size = dim(carr); /* --- Bubble Sort --- */ do i = 1 to size - 1; do j = i+1 to size; if carr(i) > carr(j) then do; tempc = carr(i); carr(i) = carr(j); carr(j) = tempc; end; end; end; /* Array carr is now sorted in ascending order */ endmethod; EndClass;
This example creates a new instance of the Sort class and sends a message to the sort method to sort the order of the existing arrays CARR and NARR.
Init: DCL Char(20) carr(3)=('c','b','a'); DCL Num narr(3)={3, 2, 1}; DCL Sort obj = _NEW_ Sort(); obj.sort(carr); obj.sort(narr); put carr= narr=;The output is
carr= carr[1]='a' carr[2]='b' carr[3]='c' narr[1] = 1 narr[2] = 2 narr[3] = 3
Add a variable number of numbers, and print out the sum. The method ignores any character types that are passed in.
Class Varying; SUMPRINT: method msg:Char REST=rest_list; DCL num totsum; if rest_list = . then do; put 'No numbers to add were passed in!'; return; end; totsum = 0; do i = 1 to listlen( rest_list ); type = itemtype( rest_list, i ); if ( type = 'N' ) then do; valn = getitemn( rest_list, i ); totsum = totsum + valn; end; end; put msg totsum; endmethod; EndClass;
Use the following program to invoke the SUMPRINT method:
Init: DCL Varying obj = _NEW_ Varying(); obj.SUMPRINT('The total is:', 15, 30, 1);
The output of this example is
The total is: 46
This program shows the parameters of the same name and different types being used across different method statements.
Class ReUseName; m1: Method n:Num c:Char; DCL Num localN; DCL Char localC; EndMethod; m2: Method n:Char c:num; DCL Char localN; DCL Num localC; EndMethod; EndClass;
See Also |
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.