DIM: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
m (Protected "DIM" ([Edit=Allow only autoconfirmed users] (indefinite) [Move=Allow only autoconfirmed users] (indefinite)))
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 4: Line 4:
{{PageSyntax}}
{{PageSyntax}}
::''To declare variables:''
::''To declare variables:''
:::[[DIM]] [{{KW|SHARED}}] ''variable''[{suffix| {{KW|AS}} [{{KW|_UNSIGNED}}] ''type''}] [, ''variable2''...]]
:::[[DIM]] [[[SHARED]]] ''variable''[{suffix| [[AS]] [[[_UNSIGNED]]] ''type''}] [, ''variable2''...]]


::''To declare arrays:''
::''To declare arrays:''
:::[[DIM]] [{{KW|SHARED}}] ''array([lowest% {{KW|TO}}] highest%])''[{suffix| {{KW|AS}} [{{KW|_UNSIGNED}}] ''type''}] [, ''variable2''...]
:::[[DIM]] [[[SHARED]]] ''array([lowest% [[TO]]] highest%])''[{suffix| [[AS]] [[[_UNSIGNED]]] ''type''}] [, ''variable2''...]


::'' '''QB64''' Alternative Syntax:''
::'' '''QB64''' Alternative Syntax:''
:::[[DIM]] [{{KW|SHARED}}] {{KW|AS}} [{{KW|_UNSIGNED}}] ''type'' ''variable''  [, ''variable2''...]
:::[[DIM]] [[[SHARED]]] [[AS]] [[[_UNSIGNED]]] ''type'' ''variable''  [, ''variable2''...]
:::[[DIM]] [{{KW|SHARED}}] {{KW|AS}} [{{KW|_UNSIGNED}}] ''type'' ''array([lowest% {{KW|TO}}] highest%])'' [, ''array2(elements)''...]
:::[[DIM]] [[[SHARED]]] [[AS]] [[[_UNSIGNED]]] ''type'' ''array([lowest% [[TO]]] highest%])'' [, ''array2(elements)''...]




{{PageDescription}}
{{PageDescription}}
* Sets the [[INTEGER]] range of elements (indices) of a [[STATIC]] array. If only one number is used, the [[LBOUND|lowest boundary]] is 0 by default.  
* Sets the [[INTEGER]] range of elements (indices) of a [[STATIC]] array. If only one number is used, the [[LBOUND|lowest boundary]] is 0 by default.
* When used before an array is dimensioned, '''[[OPTION BASE]] 1''' can set the default [[LBOUND|lower boundary]] of arrays to 1.
* When used before an array is dimensioned, '''[[OPTION BASE]] 1''' can set the default [[LBOUND|lower boundary]] of arrays to 1.
* DIM [[SHARED]] shares variable values with sub-procedures without passing the value in a parameter.
* DIM [[SHARED]] shares variable values with sub-procedures without passing the value in a parameter.
Line 24: Line 24:
** [[DOUBLE]] (or use variable suffix '''#''')
** [[DOUBLE]] (or use variable suffix '''#''')
** [[STRING]] (or use variable suffix '''$'''). An AS multiplier can set the string [[LEN|length]]. Ex: {{InlineCode}}DIM ''variable'' AS STRING * 8{{InlineCodeEnd}}
** [[STRING]] (or use variable suffix '''$'''). An AS multiplier can set the string [[LEN|length]]. Ex: {{InlineCode}}DIM ''variable'' AS STRING * 8{{InlineCodeEnd}}
* '''QB64''' variable types:  
* '''QB64''' variable types:
** [[_BIT]] (or use variable suffix '''`'''). An AS multiplier can be used for multiple bits. Ex: {{InlineCode}}DIM ''variable'' AS _BIT * 8{{InlineCodeEnd}}
** [[_BIT]] (or use variable suffix '''`'''). An AS multiplier can be used for multiple bits. Ex: {{InlineCode}}DIM ''variable'' AS _BIT * 8{{InlineCodeEnd}}
** [[_BYTE]] (or use variable suffix '''%%''')
** [[_BYTE]] (or use variable suffix '''%%''')
Line 34: Line 34:
* When using the '''AS type variable-list''' syntax, type symbols cannot be used.
* When using the '''AS type variable-list''' syntax, type symbols cannot be used.
* When the [[$DYNAMIC]] metacommand or [[REDIM]] is used, array element sizes are changeable (not [[$STATIC]]).
* When the [[$DYNAMIC]] metacommand or [[REDIM]] is used, array element sizes are changeable (not [[$STATIC]]).
* Use [[REDIM]] instead of DIM to dimension arrays as dynamic without the {{KW|$DYNAMIC}} metacommand.
* Use [[REDIM]] instead of DIM to dimension arrays as dynamic without the [[$DYNAMIC]] metacommand.
* Use [[REDIM]] [[_PRESERVE]] in '''QB64''' to retain previous array values when changing the size of an array.  
* Use [[REDIM]] [[_PRESERVE]] in '''QB64''' to retain previous array values when changing the size of an array.
* [[REDIM]] [[_PRESERVE]] cannot change the number of array dimensions. An [[ERROR Codes|error]] will occur.
* [[REDIM]] [[_PRESERVE]] cannot change the number of array dimensions. An [[ERROR Codes|error]] will occur.
* [[$DYNAMIC|Dynamic]] arrays MUST be [[REDIM]]ensioned if [[ERASE]] or [[CLEAR]] are used, as the arrays are completely removed.
* [[$DYNAMIC|Dynamic]] arrays MUST be [[REDIM]]ensioned if [[ERASE]] or [[CLEAR]] are used, as the arrays are completely removed.
* All numerical variable types '''except''' {{KW|SINGLE}}, {{KW|DOUBLE}} and {{KW|_FLOAT}} can be dimensioned as [[_UNSIGNED]] (suffix ~) or positive only.
* All numerical variable types '''except''' [[SINGLE]], [[DOUBLE]] and [[_FLOAT]] can be dimensioned as [[_UNSIGNED]] (suffix ~) or positive only.
* '''NOTE:''' Many QBasic keyword variable names can be used with a [[STRING]] suffix ($). You cannot use them without the suffix, use a numerical suffix or use ''DIM, [[REDIM]], [[_DEFINE]], [[BYVAL]] or [[TYPE]] variable [[AS]]'' statements. '''Although possible, it's recommended to avoid using reserved names.'''
* '''NOTE:''' Many QBasic keyword variable names can be used with a [[STRING]] suffix ($). You cannot use them without the suffix, use a numerical suffix or use ''DIM, [[REDIM]], [[_DEFINE]], [[BYVAL]] or [[TYPE]] variable [[AS]]'' statements. '''Although possible, it's recommended to avoid using reserved names.'''
* '''Warning: Do not use negative array upper bound index values, or OS access or "Out of Memory" [[ERROR Codes|errors]] will occur.'''
* '''Warning: Do not use negative array upper bound index values, or OS access or "Out of Memory" [[ERROR Codes|errors]] will occur.'''
Line 46: Line 46:
''Example 1:'' Defines Qt variable as a one byte fixed length string.
''Example 1:'' Defines Qt variable as a one byte fixed length string.
{{CodeStart}}
{{CodeStart}}
  {{Cl|DIM}} Qt {{Cl|AS}} {{Cl|STRING}} * 1  
  {{Cl|DIM}} Qt {{Cl|AS}} {{Cl|STRING}} * 1
{{CodeEnd}}
{{CodeEnd}}


''Example 2:'' Dimensions and types an array.  
''Example 2:'' Dimensions and types an array.
{{CodeStart}}
{{CodeStart}}
  {{Cl|DIM}} Image(2000) {{Cl|AS}} {{Cl|INTEGER}}
  {{Cl|DIM}} Image(2000) {{Cl|AS}} {{Cl|INTEGER}}
Line 56: Line 56:
''Example 3:'' Dimensions array with an [[INTEGER]] type suffix.
''Example 3:'' Dimensions array with an [[INTEGER]] type suffix.
{{CodeStart}}
{{CodeStart}}
  {{Cl|DIM}} Image%(2000)
  {{Cl|DIM}} Image%(2000)
{{CodeEnd}}
{{CodeEnd}}


''Example 4:'' Dimensions a range of [[Arrays|array]] elements as [[SHARED]] integers.
''Example 4:'' Dimensions a range of [[Arrays|array]] elements as [[SHARED]] integers.
{{CodeStart}}
{{CodeStart}}
  {{Cl|DIM}} {{Cl|SHARED}} Image(1 {{Cl|TO}} 1000) {{Cl|AS}} {{Cl|INTEGER}}  
  {{Cl|DIM}} {{Cl|SHARED}} Image(1 {{Cl|TO}} 1000) {{Cl|AS}} {{Cl|INTEGER}}
{{CodeEnd}}
{{CodeEnd}}


''Example 5:'' Dimensions variable as an [[Arrays|array]] of 8 elements of the type [[UNSIGNED]] BIT.
''Example 5:'' Dimensions variable as an [[Arrays|array]] of 8 elements of the type [[UNSIGNED]] BIT.
{{CodeStart}}
{{CodeStart}}
  {{Cl|DIM}} bit(8) {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_BIT}}  
  {{Cl|DIM}} bit(8) {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_BIT}}
{{CodeEnd}}
{{CodeEnd}}




''Example 6:'' QB64 is more flexible than QBasic when it comes to "Duplicate Definition" errors. The following code does not error:
''Example 6:'' QB64 is more flexible than QBasic when it comes to "Duplicate Definition" errors. The following code does not error:
{{CodeStart}} '' ''
{{CodeStart}}
x = 1 'x is a {{Cl|SINGLE}} variable
x = 1 'x is a {{Cl|SINGLE}} variable
{{Cl|PRINT}} x
{{Cl|PRINT}} x
{{Cl|DIM}} x {{Cl|AS}} {{Cl|LONG}}
{{Cl|DIM}} x {{Cl|AS}} {{Cl|LONG}}
{{Cl|PRINT}} x '' ''
{{Cl|PRINT}} x
{{CodeEnd}}
{{CodeEnd}}
: ''Explanation:'' The [[SINGLE]] variable can be differentiated from the [[LONG]] x variable by using suffixes like x! or x& in later code.
: ''Explanation:'' The [[SINGLE]] variable can be differentiated from the [[LONG]] x variable by using suffixes like x! or x& in later code.
Line 81: Line 81:


''Example 7:'' The following code will create a "Name already in use" '''status error''' in QB64 when the variable types are the same.
''Example 7:'' The following code will create a "Name already in use" '''status error''' in QB64 when the variable types are the same.
{{CodeStart}} '' ''
{{CodeStart}}
x = 1 'x is a {{Cl|SINGLE}} variable
x = 1 'x is a {{Cl|SINGLE}} variable
{{Cl|PRINT}} x
{{Cl|PRINT}} x
{{Cl|DIM}} x {{Cl|AS}} {{Cl|SINGLE}}
{{Cl|DIM}} x {{Cl|AS}} {{Cl|SINGLE}}
{{Cl|PRINT}} x '' ''
{{Cl|PRINT}} x
{{CodeEnd}}
{{CodeEnd}}
: ''Explanation:'' QB64 gives an error because the creation of the new variable would make referring to the existing one impossible.
: ''Explanation:'' QB64 gives an error because the creation of the new variable would make referring to the existing one impossible.
Line 91: Line 91:


''Example 8:'' Using QB64's alternative syntax to declare multiple variables/arrays of the same type.
''Example 8:'' Using QB64's alternative syntax to declare multiple variables/arrays of the same type.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|DIM}} {{Cl|AS}} {{Cl|LONG}} w, h, id, weight, index 'all of these variables are created as type LONG
{{Cl|DIM}} {{Cl|AS}} {{Cl|LONG}} w, h, id, weight, index 'all of these variables are created as type LONG
{{Cl|DIM}} {{Cl|AS}} {{Cl|SINGLE}} x, y, z              'all of these variables are created as type SINGLE
{{Cl|DIM}} {{Cl|AS}} {{Cl|SINGLE}} x, y, z              'all of these variables are created as type SINGLE

Latest revision as of 00:48, 25 January 2023

The DIM statement is used to declare a variable or a list of variables as a specified data type or to dimension $STATIC or $DYNAMIC arrays.


Syntax

To declare variables:
DIM [[[SHARED]]] variable[{suffix| AS [[[_UNSIGNED]]] type}] [, variable2...]]
To declare arrays:
DIM [[[SHARED]]] array([lowest% TO] highest%])[{suffix| AS [[[_UNSIGNED]]] type}] [, variable2...]
QB64 Alternative Syntax:
DIM [[[SHARED]]] AS [[[_UNSIGNED]]] type variable [, variable2...]
DIM [[[SHARED]]] AS [[[_UNSIGNED]]] type array([lowest% TO] highest%]) [, array2(elements)...]


Description

  • Sets the INTEGER range of elements (indices) of a STATIC array. If only one number is used, the lowest boundary is 0 by default.
  • When used before an array is dimensioned, OPTION BASE 1 can set the default lower boundary of arrays to 1.
  • DIM SHARED shares variable values with sub-procedures without passing the value in a parameter.
  • Use the AS keyword to define a variable or array type AS...
    • INTEGER (or use variable suffix %)
    • LONG (or use variable suffix &)
    • SINGLE (or use variable suffix ! or no suffix by default)
    • DOUBLE (or use variable suffix #)
    • STRING (or use variable suffix $). An AS multiplier can set the string length. Ex: DIM variable AS STRING * 8
  • QB64 variable types:
    • _BIT (or use variable suffix `). An AS multiplier can be used for multiple bits. Ex: DIM variable AS _BIT * 8
    • _BYTE (or use variable suffix %%)
    • _INTEGER64 (or use variable suffix &&)
    • _FLOAT (or use variable suffix ##)
    • _OFFSET (or use variable suffix %&)
    • DIM AS _MEM (the _MEM type has no type suffix).
  • Note: When a variable has not been defined or has no type suffix, the type defaults to SINGLE.
  • When using the AS type variable-list syntax, type symbols cannot be used.
  • When the $DYNAMIC metacommand or REDIM is used, array element sizes are changeable (not $STATIC).
  • Use REDIM instead of DIM to dimension arrays as dynamic without the $DYNAMIC metacommand.
  • Use REDIM _PRESERVE in QB64 to retain previous array values when changing the size of an array.
  • REDIM _PRESERVE cannot change the number of array dimensions. An error will occur.
  • Dynamic arrays MUST be REDIMensioned if ERASE or CLEAR are used, as the arrays are completely removed.
  • All numerical variable types except SINGLE, DOUBLE and _FLOAT can be dimensioned as _UNSIGNED (suffix ~) or positive only.
  • NOTE: Many QBasic keyword variable names can be used with a STRING suffix ($). You cannot use them without the suffix, use a numerical suffix or use DIM, REDIM, _DEFINE, BYVAL or TYPE variable AS statements. Although possible, it's recommended to avoid using reserved names.
  • Warning: Do not use negative array upper bound index values, or OS access or "Out of Memory" errors will occur.


Examples

Example 1: Defines Qt variable as a one byte fixed length string.

 DIM Qt AS STRING * 1

Example 2: Dimensions and types an array.

 DIM Image(2000) AS INTEGER

Example 3: Dimensions array with an INTEGER type suffix.

 DIM Image%(2000)

Example 4: Dimensions a range of array elements as SHARED integers.

 DIM SHARED Image(1 TO 1000) AS INTEGER

Example 5: Dimensions variable as an array of 8 elements of the type UNSIGNED BIT.

 DIM bit(8) AS _UNSIGNED _BIT


Example 6: QB64 is more flexible than QBasic when it comes to "Duplicate Definition" errors. The following code does not error:

x = 1 'x is a SINGLE variable
PRINT x
DIM x AS LONG
PRINT x
Explanation: The SINGLE variable can be differentiated from the LONG x variable by using suffixes like x! or x& in later code.


Example 7: The following code will create a "Name already in use" status error in QB64 when the variable types are the same.

x = 1 'x is a SINGLE variable
PRINT x
DIM x AS SINGLE
PRINT x
Explanation: QB64 gives an error because the creation of the new variable would make referring to the existing one impossible.


Example 8: Using QB64's alternative syntax to declare multiple variables/arrays of the same type.

DIM AS LONG w, h, id, weight, index 'all of these variables are created as type LONG
DIM AS SINGLE x, y, z               'all of these variables are created as type SINGLE


See also



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