SHARED: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
m (Removed protection from "SHARED")
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
The '''SHARED''' statement allows variables to be passed automatically to any [[SUB]] or [[FUNCTION]] procedure.
The '''SHARED''' statement allows variables to be passed automatically to any [[SUB]] or [[FUNCTION]] procedure.




Line 8: Line 7:


* [[DIM]]ensioned variables are shared with all procedures in the program module.
* [[DIM]]ensioned variables are shared with all procedures in the program module.
* When used with [[DIM]] in the main module, it eliminates the need to pass a parameter variable to a [[SUB]] or [[FUNCTION]].  
* When used with [[DIM]] in the main module, it eliminates the need to pass a parameter variable to a [[SUB]] or [[FUNCTION]].
* Use [[COMMON SHARED]] to share a list of variable values with sub-procedures or other modules. See also: [[COMMON]]
* Use [[COMMON SHARED]] to share a list of variable values with sub-procedures or other modules. See also: [[COMMON]]
* SHARED ('''without [[DIM]]''') can share a list of variables inside of [[SUB]] or [[FUNCTION]] procedures with the main module only.
* SHARED ('''without [[DIM]]''') can share a list of variables inside of [[SUB]] or [[FUNCTION]] procedures with the main module only.
Line 15: Line 14:


''Example 1:'' Defining variable types with [[AS]] or type suffixes.
''Example 1:'' Defining variable types with [[AS]] or type suffixes.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|DIM}} {{Cl|SHARED}} Qt AS {{Cl|STRING}} * 1, price AS {{Cl|DOUBLE}}, ID AS {{Cl|INTEGER}}
{{Cl|DIM}} {{Cl|SHARED}} Qt AS {{Cl|STRING}} * 1, price AS {{Cl|DOUBLE}}, ID AS {{Cl|INTEGER}}
{{Cl|DIM}} {{Cl|SHARED}} Q$, prices#, IDs%
{{Cl|DIM}} {{Cl|SHARED}} Q$, prices#, IDs%
Line 22: Line 21:


''Example 2:'' The DIR$ function returns a filename or a list when more than one exist. The file spec can use a path and/or wildcards.
''Example 2:'' The DIR$ function returns a filename or a list when more than one exist. The file spec can use a path and/or wildcards.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} 2
{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} 2
   {{Cl|LINE INPUT}} "Enter a file spec: ", spec$
   {{Cl|LINE INPUT}} "Enter a file spec: ", spec$
   file$ = DIR$(spec$)        'use a file spec ONCE to find the last file name listed
   file$ = DIR$(spec$)        'use a file spec ONCE to find the last file name listed
   {{Cl|PRINT}} DIRCount%, file$,    'function can return the file count using SHARED variable  
   {{Cl|PRINT}} DIRCount%, file$,    'function can return the file count using SHARED variable
   {{Cl|DO}}
   {{Cl|DO}}
     K$ = {{Cl|INPUT$}}(1)
     K$ = {{Cl|INPUT$}}(1)
Line 44: Line 43:
   Index% = 0: DirList$(Index%) = "": ff% = {{Cl|FREEFILE}}
   Index% = 0: DirList$(Index%) = "": ff% = {{Cl|FREEFILE}}
   {{Cl|OPEN}} TmpFile$ {{Cl|FOR (file statement)|FOR}} {{Cl|APPEND}} {{Cl|AS}} #ff%
   {{Cl|OPEN}} TmpFile$ {{Cl|FOR (file statement)|FOR}} {{Cl|APPEND}} {{Cl|AS}} #ff%
   size& = {{Cl|LOF}}(ff%)  
   size& = {{Cl|LOF}}(ff%)
   {{Cl|CLOSE}} #ff%
   {{Cl|CLOSE}} #ff%
   {{Cl|IF}} size& = 0 {{Cl|THEN}} {{Cl|KILL}} TmpFile$: {{Cl|EXIT FUNCTION}}
   {{Cl|IF}} size& = 0 {{Cl|THEN}} {{Cl|KILL}} TmpFile$: {{Cl|EXIT FUNCTION}}
   {{Cl|OPEN}} TmpFile$ {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #ff%    
   {{Cl|OPEN}} TmpFile$ {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #ff%
   {{Cl|DO}} {{Cl|WHILE}} {{Cl|NOT}} {{Cl|EOF}}(ff%) {{Cl|AND (boolean)|AND}} Index% < ListMAX%
   {{Cl|DO}} {{Cl|WHILE}} {{Cl|NOT}} {{Cl|EOF}}(ff%) {{Cl|AND (boolean)|AND}} Index% < ListMAX%
     Index% = Index% + 1
     Index% = Index% + 1
     {{Cl|LINE INPUT (file statement)|LINE INPUT}} #ff%, DirList$(Index%)  
     {{Cl|LINE INPUT (file statement)|LINE INPUT}} #ff%, DirList$(Index%)
   {{Cl|LOOP}}
   {{Cl|LOOP}}
   DIRCount% = Index%                      'SHARED variable can return the file count
   DIRCount% = Index%                      'SHARED variable can return the file count
Line 56: Line 55:
   {{Cl|KILL}} TmpFile$
   {{Cl|KILL}} TmpFile$
{{Cl|ELSE}} {{Cl|IF}} Index% > 0 {{Cl|THEN}} Index% = Index% - 1 'no spec sends next file name
{{Cl|ELSE}} {{Cl|IF}} Index% > 0 {{Cl|THEN}} Index% = Index% - 1 'no spec sends next file name
{{Cl|END IF}}                                    
{{Cl|END IF}}
DIR$ = DirList$(Index%)
DIR$ = DirList$(Index%)
{{Cl|END FUNCTION}} '' ''
{{Cl|END FUNCTION}}
{{CodeEnd}}{{small|Code by Ted Weissgerber}}
{{CodeEnd}}
{{Small|Code by Ted Weissgerber}}
: ''Explanation:'' The SHARED variable value ''DIRcount%'' can tell the main program how many files were found using a wildcard spec.
: ''Explanation:'' The SHARED variable value ''DIRcount%'' can tell the main program how many files were found using a wildcard spec.




''See also:''
{{PageSeeAlso}}
* [[DIM]], [[REDIM]]
* [[DIM]], [[REDIM]]
* [[COMMON]], [[COMMON SHARED]]
* [[COMMON]], [[COMMON SHARED]]

Latest revision as of 22:48, 11 February 2023

The SHARED statement allows variables to be passed automatically to any SUB or FUNCTION procedure.


Syntax

DIM SHARED Qt AS STRING * 1


  • DIMensioned variables are shared with all procedures in the program module.
  • When used with DIM in the main module, it eliminates the need to pass a parameter variable to a SUB or FUNCTION.
  • Use COMMON SHARED to share a list of variable values with sub-procedures or other modules. See also: COMMON
  • SHARED (without DIM) can share a list of variables inside of SUB or FUNCTION procedures with the main module only.
Note: SHARED variables in sub-procedures will not be passed to other sub-procedures, only the main module.


Example 1: Defining variable types with AS or type suffixes.

DIM SHARED Qt AS STRING * 1, price AS DOUBLE, ID AS INTEGER
DIM SHARED Q$, prices#, IDs%


Example 2: The DIR$ function returns a filename or a list when more than one exist. The file spec can use a path and/or wildcards.

FOR i = 1 TO 2
  LINE INPUT "Enter a file spec: ", spec$
  file$ = DIR$(spec$)        'use a file spec ONCE to find the last file name listed
  PRINT DIRCount%, file$,    'function can return the file count using SHARED variable
  DO
    K$ = INPUT$(1)
    file$ = DIR$("")         'use an empty string parameter to return a list of files!
    PRINT file$,
  LOOP UNTIL LEN(file$) = 0  'file list ends with an empty string
NEXT
END

FUNCTION DIR$ (spec$)
CONST TmpFile$ = "DIR$INF0.INF", ListMAX% = 500  'change maximum to suit your needs
SHARED DIRCount%                                 'returns file count if desired
STATIC Ready%, Index%, DirList$()
IF NOT Ready% THEN REDIM DirList$(ListMax%): Ready% = -1  'DIM array first use
IF spec$ > "" THEN                               'get file names when a spec is given
  SHELL _HIDE "DIR " + spec$ + " /b > " + TmpFile$
  Index% = 0: DirList$(Index%) = "": ff% = FREEFILE
  OPEN TmpFile$ FOR APPEND AS #ff%
  size& = LOF(ff%)
  CLOSE #ff%
  IF size& = 0 THEN KILL TmpFile$: EXIT FUNCTION
  OPEN TmpFile$ FOR INPUT AS #ff%
  DO WHILE NOT EOF(ff%) AND Index% < ListMAX%
    Index% = Index% + 1
    LINE INPUT #ff%, DirList$(Index%)
  LOOP
  DIRCount% = Index%                       'SHARED variable can return the file count
  CLOSE #ff%
  KILL TmpFile$
ELSE IF Index% > 0 THEN Index% = Index% - 1 'no spec sends next file name
END IF
DIR$ = DirList$(Index%)
END FUNCTION
Code by Ted Weissgerber
Explanation: The SHARED variable value DIRcount% can tell the main program how many files were found using a wildcard spec.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link