Static
Static <.varname.>
 
Parameters: NONE
Returns: NONE
 
     Static variables are a special form of local variable within Functions. Just like the Local directive, Static declarations affect the visibility of variables. However, local variables (those inside functions) will be initialised every time the Function is called, a static variable, will keep its current value/string between calls.

      Static supports three basic forms, You can simply define a single static, a list of statics using comas to separate them, or define a static with an assignment.
i.e.

  
  Static MyVariable
  Static Monday,Tuesday,Wednesday,Thursday,Friday
  Static Score = 1000
  


      Note: Variables defined as Static will have precedence over any variable of the same name, that's defined in main program as global.



FACTS:


      * Static variables can only be defined within a Function block

      * Static can handle assignments. I.e. (Static Score=1000)

      * Static can handle lists by using coma's between them. I.e. (Static Monday,Tuesay,Wednesday,Thursday,Friday)

      * Static Variables have precedence over any variable of the same name that was defined in main program as global.



Mini Tutorial :

      This simple examples is meant to show the difference between Local and Static variables

  
; a function with local and static variable
Function LocalStatic(aValue)
  Local MyLocal
  Static MyStatic
  
; the initial value of MyLocal will always be 0
  Print "The initial value of MyLocal = " + Str$(MyLocal)
  MyLocal = MyLocal + aValue
  Print "MyLocal + " + Str$(aValue) + " = " + Str$(MyLocal)
  
; the value of MyStatic is kept between each function calls
  Print "The initial value of MyStatic = " + Str$(MyStatic)
  MyStatic = MyStatic + aValue
  Print "MyStatic + " + Str$(aValue) + " = " + Str$(MyStatic)
  
EndFunction
  
  
  For i = 1 To 3
     LocalStatic(i)
     Print ""
     Sync
  Next i
  
  WaitKey
  


     The output of this example would be:

  
  The initial value of MyLocal = 0
  MyLocal + 1 = 1
  The initial value of MyStatic = 0
  MyStatic + 1 = 1
  
  The initial value of MyLocal = 0
  MyLocal + 2 = 2
  The initial value of MyStatic = 1
  MyStatic + 2 = 3
  
  The initial value of MyLocal = 0
  MyLocal + 3 = 3
  The initial value of MyStatic = 3
  MyStatic + 3 = 6
  




 
Related Info: Constant | Dim | Explicit | Function | Functions&Psub | Global | Local | Psub | Variables :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com