Announcement

Collapse
No announcement yet.

Global Statement

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Global Statement

    Please add your comments and suggestions as a Reply to this thread.





    PB/WIN - GLOBAL statement

    Purpose
    Declare global (shared) variables between Subs, Functions. Methods, and Properties.
    Syntax
    GLOBAL variable[()] [ AS type] [, variable[()]] [, ...]
    GLOBAL variable[()] [, variable[()]] [, ...] AS type
    Remarks
    GLOBAL declares the specified variable(s) as global to the entire program. This gives a procedure access to variable(s), without having to pass them as parameters. To declare an array as a global variable, use an empty set of parentheses in the variable list:

    Code:
    GLOBAL MyArray%()
    GLOBAL StringArray() AS STRING
    You must then use the DIM or REDIM statements to dimension the array inside a procedure. A good place to do this is inside your WINMAIN or PBMAIN function.

    If an array is defined as GLOBAL outside a procedure, you should include the GLOBAL keyword in the DIM statement for clarity, and compatibility with future versions of PowerBASIC:

    Code:
    GLOBAL a() AS STRING
    FUNCTION PBMAIN
      DIM a(1 TO 500) AS GLOBAL STRING
      [statements]
    END FUNCTION
    The GLOBAL statement may accept a list of variables, all of which are defined by the type descriptor keywords which follow them. For example:

    Code:
    GLOBAL aaa, bbb, ccc AS INTEGER
    GLOBAL vptr, aptr() AS LONG PTR
    Restrictions
    GLOBAL variables are not shared between programs and DLLs or between multiple instances of the same DLL. That is, a GLOBAL variable is only global within its own module. The simplest way to expose a variable to a DLL is to pass the variable BYREF (by reference) to the target DLL. DEFtype has no effect on variables defined by a GLOBAL statement.
    See AlsoReferencesExamples
    Code:
    #COMPILE EXE
    GLOBAL Caption AS ASCIIZ * 255
    FUNCTION PBMAIN() AS LONG
      DIM Msg AS ASCIIZ * 255
      CALL SetVars
      IF Caption = "GLOBAL test" then Msg = "Success!"
    END FUNCTION
    SUB SetVars()
      Caption = "GLOBAL test"
    END SUB
    Last edited by Gary Beene; 28 Oct 2014, 09:04 PM.

  • #2
    See discussion on duplicate Globals here.

    Comment

    Working...
    X