protect ...

Prev Next

Function Names

protect, protect recursive

Description

This function applies a protection setting on the variables. Protection can only be applied on local variables only if running in a local variable context (e.g. inside a user-function or running a B4P file started from another B4P file using start() or include(). Exceptions can be enabled using global(). Changing protection on a system variable is not possible at all and any time.

Attention: If protecting a member variable, do not forget to protect the parents, too (If parents are in danger, then the members are, too, even if they are protected).

The "protect recursive" call will apply the protection setting to all member variables.
Note that "protect recursive (check)" only checks protection settings in the base variable.

Note: Member variables created after protection settings made do not inherit any protection settings from their parents. full access is assumed initially.

Call as: procedure or function

Restrictions

Indirect parameter passing is disabled

Parameter count

Min 2

Parameters

No.TypeDescription
1
input
string Protection setting

Following protection settings are available:

tightly locked Entire variable incl. members locked: Reading writing and deleting is forbidden
locked Reading, writing and deleting is forbidden
read only Writing and deleting is forbidden
limited access Values can be modified, but not the type (e.g. writing a string to a variable containing a numeral). For some system variables, only specific values are allowed. No members can be created.
prevent deleting Reading and writing is allowed, but not deleting. Members may be introduced.
full access Unrestricted access
check Poll the current protection setting of the variable specified in the 2nd function parameter. This call even works if the variable is locked.

2, etc.
code
variable
:string
Variable to protect

Return value

TypeDescription
string Polled protection setting

The protection setting of the 1st variable is returned.

Examples

       b[] = 2;
       b[Hi] = 3;
       b[Ho] = 4;

       protect( read only, b[] ) ;

       echo( "Base variable protection setting: ", protect( check, b[] ) );
       see( b[] );

       protect recursive( prevent deleting, b[] ) ;

       echo( "Base variable protection setting: ", protect( check, b[] ) );
       see( b[] );
       b[] = 2;
       b[Hi] = 3;
       b[Ho] = 4;

       protect( read only, b[] ) ;

       echo( "Base variable protection setting: ", protect( check, b[] ) );
       see( b[] );

       protect recursive( prevent deleting, b[] ) ;

       echo( "Base variable protection setting: ", protect( check, b[] ) );
       see( b[] );

Output

Base variable protection setting: read only
b[]                     2  "2"                     (numeral,read only)
Hi                      3  "3"                     (numeral,full access)
Ho                      4  "4"                     (numeral,full access)

Base variable protection setting: prevent deleting
b[]                     2  "2"                     (numeral,prevent deleting)
Hi                      3  "3"                     (numeral,prevent deleting)
Ho                      4  "4"                     (numeral,prevent deleting)

Base variable protection setting: read only
b[]                     2  "2"                     (numeral,read only)
Hi                      3  "3"                     (numeral,prevent deleting)
Ho                      4  "4"                     (numeral,prevent deleting)

Base variable protection setting: prevent deleting
b[]                     2  "2"                     (numeral,prevent deleting)
Hi                      3  "3"                     (numeral,prevent deleting)
Ho                      4  "4"                     (numeral,prevent deleting)

Try it yourself: Open LIB_Function_protect.b4p in B4P_Examples.zip. Decompress before use.