PUBLIC |
|
|
The PUBLIC statement defines public property variables, subroutines and functions in a class module.
Format
PUBLIC var {READONLY} {= value}, mat(rows, cols), ...
PUBLIC SUBROUTINE name{(arg1, arg2)} {VAR.ARGS} ...statements... END
PUBLIC FUNCTION name{(arg1, arg2)} {VAR.ARGS} ...statements... END
where
Note that the equivalence of a function to a subroutine with a hidden first argument as found with the SUBROUTINE and FUNCTION statements does not apply to public subroutines and functions.
The first form of the PUBLIC statement defines persistent variables that may be visible from outside the object in which they appear. Public variables are initially unassigned unless an initialisation value is present. If used, value must be a numeric or string constant, a CHAR() or ECHAR() function with a numeric constant argument, or a constant @-variable name. An initialisation value may be provided for a mat declaration and is equivalent to use of the MAT statement to set all elements to the given value. The initialisation is applied prior to execution of the CREATE.OBJECT subroutine, if present.
The second and third forms of the PUBLIC statement define a subroutine or function that can be referenced from outside the object. The synonyms GET and SET may be used for PUBLIC FUNCTION and PUBLIC SUBROUTINE respectively.
Arguments to public subroutines and functions may reference a whole matrix by following the matrix name by its dimensions. The actual values given are ignored; the compiler simply counts them to determine whether the matrix has one or two dimensions. For example: PUBLIC SUBROUTINE CALC(CLIENT, CLI.REC(1), TOTVAL) In this example, the dimension value has been shown as 1 to emphasise that the actual value is irrelevant. The compiler uses this purely to determine that CLI.REC is a single dimensional matrix, possibly representing a database record read using MATREAD. The alternative syntax used with SUBROUTINE statements by prefixing the matrix name with MAT and using a DIMENSION statement to set dimensionality is not available for public subroutines and functions.
The number of arguments in calls to the subroutine or function must be the same as in the declaration unless the VAR.ARGS option is used in which case any arguments not passed by the caller will be unassigned. The ARG.COUNT() function can be used to determine the actual number of arguments passed, excluding the hidden return argument. The ARG.PRESENT() function can be used to test for the presence of an optional argument by name. PUBLIC FUNCTION CREDIT.RATING(CLIENT, CLASS, CODE) VAR.ARGS In this example, if the calling program supplies only one argument, the CLASS and CODE variables will be unassigned. If the calling program provides two arguments, the CODE variable will be unassigned.
When using VAR.ARGS, default values may be provided for any arguments by following the argument name with an = sign and the required numeric or string value or the @FALSE or @TRUE constants. For example, PUBLIC FUNCTION CREDIT.RATING(CLIENT, CLASS = 1, CODE = "Standard") VAR.ARGS In this example, if the calling program supplies only one argument, the CLASS variable will default to 1 and the CODE variable will default to "Standard". If the calling program provides two arguments, the default value for CLASS is ignored and the CODE variable defaults to "Standard".
Examples
PUBLIC FUNCTION CONNECT(SERVER, PORT) SKT = OPEN.SOCKET(SERVER, PORT, SKT$BLOCKING) RETURN STATUS() = 0 END
The above function takes a fixed length list of two arguments and uses the supplied values to open a socket connection to a remote server. The SKT variable in this example would be a private variable within the class module.
PUBLIC FUNCTION CONNECT(SERVER, PORT) VAR.ARGS IF UNASSIGNED(PORT) THEN PORT = 4000 SKT = OPEN.SOCKET(SERVER, PORT, SKT$BLOCKING) RETURN STATUS() = 0 END
This example extends the previous one by making the PORT argument optional and, if it is not supplied by the caller, defaulting it to 4000.
PUBLIC SUBROUTINE INSERT.ITEMS(ID, ...) READU REC FROM FVAR, ID ELSE NULL FOR I = 2 TO ARG.COUNT() VALUE = ARG(I) LOCATE VALUE IN REC<1> BY 'AL' SETTING POS ELSE INS VALUE BEFORE REC<POS> END NEXT I WRITE REC TO FVAR, ID END
This example uses the ... syntax to specify a variable length argument list of the maximum permissible length. It reads a record identified by the ID argument and then inserts all items from the remaining arguments that are not already in the record.
See also: Object oriented programming, CLASS, DISINHERIT, INHERIT, OBJECT(), PRIVATE, RESTRICTED, SHARED |