PUBLIC

PUBLIC

Top  Previous  Next

 

The PUBLIC statement defines public property variables, subroutines and functions in a class module.

 

 

Format

 

PUBLIC var, mat(rows, cols), ...

 

PUBLIC SUBROUTINE name{(arg1, arg2)} {VAR.ARGS}

...statements...

END

 

PUBLIC FUNCTION name{(arg1, arg2)} {VAR.ARGS}

...statements...

END

 

where

 

varis a simple scalar variable. The variable name may be followed by READONLY to indicate that external references to the variable may not update it.

 

mat(rows, cols)is a dimensioned matrix name. The rows and cols values must be numeric constants. The dimension values may be followed by READONLY to indicate that external references to the variable may not update it.

 

name(arg1, arg2)is the subroutine or function name and an optional list of arguments. See the CLASS statement for the maximum number of arguments allowed in this list.  Specifying the final argument name as three periods (...) effectively extends the argument list to the maximum permissible length with unnamed scalar arguments that may be accessed using the ARG() function. Use of this syntax automatically implies the VAR.ARGS option which must not also be present.

 

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.

 

 

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, SHARED