Protected


Syntax
Protected[.<type>] <variable[.<type>]> [= <expression>] [, ...]
Description
Protected allows a variable to be accessed only in a Procedure even if the same variable has been declared as Global in the main program. Protected in its function is often known as 'Local' from other BASIC dialects. Each variable can have a default value directly assigned to it. If a type is specified after Protected, the default type is changed for this declaration. Protected can also be used with arrays, lists and maps.

The value of the local variable will be reinitialized at each procedure call. To avoid this, you can use the keyword Static, to separate global from local variables while keeping their values.

Example: With variable

  Global a
  a = 10
  
  Procedure Change()
    Protected a
    a = 20 
  EndProcedure 
  
  Change()
  Debug a ; Will print 10, as the variable has been protected.

Example: With array

  Global Dim Array(2)
  Array(0) = 10
  
  Procedure Change()
    Protected Dim Array(2) ; This array is protected, it will be local.
    Array(0) = 20 
  EndProcedure 
  
  Change()
  Debug Array(0) ; Will print 10, as the array has been protected.