Difference between revisions of "$DYNAMIC"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
m (Fix link)
 
(22 intermediate revisions by 3 users not shown)
Line 1: Line 1:
The '''$DYNAMIC''' Metacommand allows the creation of dynamic(changeable) array sizes.
+
The [[$DYNAMIC]] [[Metacommand|metacommand]] allows the creation of dynamic (resizable) arrays.
  
  
 +
{{PageSyntax}}
 +
:{[[REM]] | [[apostrophe|']] } [[$DYNAMIC]]
  
{{PageSyntax}} REM $DYNAMIC
 
  
 +
{{PageDescription}}
 +
* QBasic [[Metacommand|metacommands]] require [[REM]] or [[Apostrophe|apostrophe]] (') before them and are normally placed at the start of the main module.
 +
* Dynamic arrays can be resized using [[REDIM]]. The array's type cannot be changed.
 +
* All data in the array will be lost when [[REDIM]]ensioned except when [[_PRESERVE]] is used.
 +
* [[REDIM]] [[_PRESERVE]] can preserve and may move the previous array data when the array boundaries change.
 +
* [[_PRESERVE]] allows the [[UBOUND|upper]] and [[LBOUND|lower]] boundaries of an array to be changed. The number of dimensions cannot change.
 +
* [[$DYNAMIC]] arrays must be [[REDIM]]ensioned if [[ERASE]] or [[CLEAR]] are used as the arrays are removed completely.
  
* Metacommands require a REM or apostrophy (') before them.
 
* Metacommands should be placed at the program start in the main module.
 
* Dynamic arrays can be resized using {{KW|REDIM}}. The array's type cannot be changed.
 
* All data in the array will be lost when redimensioned in QBasic.
 
* In '''QB64''', REDIM {{KW|_PRESERVE}} can save previous array data.
 
* Dynamic arrays can also be resized by the program user's input if desired.
 
  
 +
{{PageExamples}}
 +
''Example:'' [[REDIM]]ing a $DYNAMIC array using [[_PRESERVE]] to retain previous array values.
 +
{{CodeStart}} '' ''
 +
{{Cl|REM}} {{Cl|$DYNAMIC}}            'create dynamic arrays only
 +
{{Cl|DIM}} array(10)            'create array with 11 elements
 +
{{Cl|FOR...NEXT|FOR}} i = 0 {{Cl|TO}} 10
 +
  array(i) = i: {{Cl|PRINT}} array(i); 'set and display element values
 +
{{Cl|NEXT}}
 +
{{Cl|PRINT}}
 +
{{Cl|REDIM}} {{Cl|_PRESERVE}} array(10 {{Cl|TO}} 20)
 +
{{Cl|FOR...NEXT|FOR}} i = 10 {{Cl|TO}} 20
 +
  {{Cl|PRINT}} array(i);
 +
{{Cl|NEXT}}
 +
{{Cl|END}} '' ''
 +
{{CodeEnd}}
 +
{{OutputStart}}0  1  2  3  4  5  6  7  8  9  10
  
 
+
0  1  2  3  4  5  6  7  8  9  10
''See also:''
+
{{OutputEnd}}
* {{KW|$STATIC}}, {{KW|$INCLUDE}}
 
* {{KW|DIM}}, {{KW|Arrays}}
 
  
  
 +
{{PageSeeAlso}}
 +
* [[$STATIC]], [[$INCLUDE]]
 +
* [[DIM]], [[REDIM]], [[_DEFINE]]
 +
* [[STATIC]]
 +
* [[ERASE]], [[CLEAR]]
 +
* [[Arrays]], [[Metacommand]]
  
  
 
{{PageNavigation}}
 
{{PageNavigation}}

Latest revision as of 15:36, 17 February 2021

The $DYNAMIC metacommand allows the creation of dynamic (resizable) arrays.


Syntax

{REM | ' } $DYNAMIC


Description

  • QBasic metacommands require REM or apostrophe (') before them and are normally placed at the start of the main module.
  • Dynamic arrays can be resized using REDIM. The array's type cannot be changed.
  • All data in the array will be lost when REDIMensioned except when _PRESERVE is used.
  • REDIM _PRESERVE can preserve and may move the previous array data when the array boundaries change.
  • _PRESERVE allows the upper and lower boundaries of an array to be changed. The number of dimensions cannot change.
  • $DYNAMIC arrays must be REDIMensioned if ERASE or CLEAR are used as the arrays are removed completely.


Examples

Example: REDIMing a $DYNAMIC array using _PRESERVE to retain previous array values.

REM $DYNAMIC 'create dynamic arrays only DIM array(10) 'create array with 11 elements FOR i = 0 TO 10 array(i) = i: PRINT array(i); 'set and display element values NEXT PRINT REDIM _PRESERVE array(10 TO 20) FOR i = 10 TO 20 PRINT array(i); NEXT END

0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10


See also



Navigation:
Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page