Explicit
Explicit State
 
Parameters:

    State = The State variable declaration ON= Explicit declaration, OFF = Implicit (default)
Returns: NONE
 

      Explicit lets the user change what variable declaration mode that the compiler uses. When enabled (Explicit ON) PlayBASIC will force the user to declare every variable prior to the variable being used. The beauty of activating Explicit mode, is that it can help you locate faults in your program that might occur due to a misspelled variable name. Such problems are common and can be very frustrating to track down.

      Moreover you can enable/disable Explicit mode anywhere you like through out your program. (see example)


State Values

      ON = Explicit declaration: Variables must be declared prior to use.

      OFF = Implicit declaration: Variables will be dynamically declared for you. This is the default variable creation mode for PlayBASIC.




FACTS:


      * Explicit can be turned ON/OFF at any time through a program. Allowing you activate it for some sections and not others.

      * Explicit parameter is optional. Explicit defaults to ON when the parameter is left off.

      * The current value of explicit mode is held in the pbExplicitState constant.




Mini Tutorial #1:


      This example demos the basics of PlayBASIC Explicit mode.

  
; -------------------------------------------------------
; NOTE: Explicit and ForceDeclaration  are interchangeable
; -------------------------------------------------------
  
; Force PlayBASIC to expect all variable to be
; declared prior to use
  
  Explicit
  
  
; explicitly declare Hello as global
  Global MyGlobalVariable=42
  
; Declare some variables to hold the screen width & height
  Dim Width As Integer
  Dim Height As Integer
  
; Get screen Width + Height
  width     =GetScreenWidth()
  Height=GetScreenHeight()
  
  
; You can also use Declaration Blocks for pure declaration
  Declaration
     MyVariable
     MyFloatVariable#
     MyStringVariable$
  EndDeclaration
  
  MyVariable               =99
  MyFloatVariable#     =123.456
  MyStringVariable$ = "Hello World"
  
  
  
  
; turn off Explicit/forced declaration mode, to allow
; dynamic variable creation again
  Explicit off
  
; Since Explicit mode is OFF, the variable 'a' will
; be dynamically created for us.
  a=a+1
  
; Turn Explicit Variable declaration ON again
  Explicit on
  
; Display the variables
  Print MyVariable
  Print MyFloatVariable#
  Print MyStringVariable$
  
  Print MyGlobalVariable
  Print Width
  Print Height
  Print a
  
; Remove the comment in front of the line bellow
; and try and compile it !.  I won't work, as
; the 'b' variable hasn't been previously
; declared
  
;   Print b
  
  
; Display the screen and wait for the user to press a key
  Sync
  WaitKey
  
  



This example would output.

  
  99
  123.456
  "Hello World"
  42
  800
  600
  1
  






Mini Tutorial #2:


      Sometimes in our programs we might want to use explicit mode in some parts and not others, so it can be very handy to ask PlayBASIC what the current state of explicit mode is at the start of block of code, then restore it later on. So if you included in the code in another programs or shared the code with other programmers, then it wouldn't change the state.

  
  
; -------------------------------------------------------
; Explicit Library Example
; -------------------------------------------------------
  
; Read the current state of Explicit mode
  Constant Previous_Explicit_Value  = pbexplicitstate
  
  
; Force PlayBASIC to expect all variable to be
; declared prior to use
  Explicit true
  
  
  
; here we'd have out library code which
; might be a function of functions or something
; but in this example we have a dumb function
; that just adds a pair of integers together
Function AddValues(A,B)
  REsult=A+B
EndFunction  Result
  
  
; Restore the explicit state to what it was
; before the compiler entered this sectio of code
  Explicit Previous_Explicit_Value
  
  





 
Related Info: Byte | Dim | Float | ForceDeclaration | Global | Integer | Local | OptExpressions | Pointer | Static | String | Type | Word :
 


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