REDIM: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
TheSnowDog (talk | contribs) m (QBasic capitalisation) Tag: visualeditor |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
A | A [[REDIM]] statement can re-dimension one [[$DYNAMIC|dynamic]](flexible) [[Arrays|array]] or a [[comma]] separated list of arrays. | ||
{{PageSyntax}} | {{PageSyntax}} | ||
:[[REDIM]] [ | :[[REDIM]] [[[_PRESERVE]]] [[[SHARED]]] ArrayName[''typesuffix''] ({''max_element''|low_element[[[TO]] ''upper_element'', ...]}) [[[AS]] [[TYPE|Type]]] | ||
Line 13: | Line 13: | ||
* {{Parameter|elements}} is the number of elements the array should hold. Use the optional [[TO]] {{Parameter|elements2}} to set a range. | * {{Parameter|elements}} is the number of elements the array should hold. Use the optional [[TO]] {{Parameter|elements2}} to set a range. | ||
* '''Always use the same array [[TYPE]] suffix ([[AS]] type) or a new array type with the same name may be created.''' | * '''Always use the same array [[TYPE]] suffix ([[AS]] type) or a new array type with the same name may be created.''' | ||
* REDIM cannot change [[$STATIC]] arrays created with a [[DIM]] statement unless the [[$DYNAMIC]] [[Metacommand]] is used! | * REDIM cannot change [[$STATIC]] arrays created with a [[DIM]] statement unless the [[$DYNAMIC]] [[Metacommand]] is used! | ||
* To create a dynamic array use the [[$DYNAMIC]] metacommand or use [[REDIM]] rather than [[DIM]] when first creating the array. | * To create a dynamic array use the [[$DYNAMIC]] metacommand or use [[REDIM]] rather than [[DIM]] when first creating the array. | ||
* Use REDIM [[_PRESERVE]] to change the range or number of array elements without losing the remaining elements. Data may move up or down to accommodate those boundary changes. | * Use REDIM [[_PRESERVE]] to change the range or number of array elements without losing the remaining elements. Data may move up or down to accommodate those boundary changes. | ||
* '''REDIM [[_PRESERVE]] cannot change the number of array dimensions or type!''' | * '''REDIM [[_PRESERVE]] cannot change the number of array dimensions or type!''' | ||
* [[$DYNAMIC|Dynamic]] arrays MUST be [[REDIM]]ensioned if [[ERASE]] or [[CLEAR]] are used to clear the arrays as they no longer exist. | * [[$DYNAMIC|Dynamic]] arrays MUST be [[REDIM]]ensioned if [[ERASE]] or [[CLEAR]] are used to clear the arrays as they no longer exist. | ||
* When [[AS]] is used to declare the type, use [[AS]] to retain that type or it will change to [[SINGLE]]! | * When [[AS]] is used to declare the type, use [[AS]] to retain that type or it will change to [[SINGLE]]! | ||
* '''NOTE: Many QBasic keyword variable names CAN be used with a [[STRING]] suffix($) ONLY! You CANNOT use them without the suffix, use a numerical suffix or use [[DIM]], [[REDIM]], [[_DEFINE]], [[BYVAL]] or [[TYPE]] variable [[AS]] statements!''' | * '''NOTE: Many QBasic keyword variable names CAN be used with a [[STRING]] suffix($) ONLY! You CANNOT use them without the suffix, use a numerical suffix or use [[DIM]], [[REDIM]], [[_DEFINE]], [[BYVAL]] or [[TYPE]] variable [[AS]] statements!''' | ||
Line 24: | Line 24: | ||
''Example 1:'' The [[$DYNAMIC]] Metacommand allows an array to be re-sized using [[DIM]] and REDIM. | ''Example 1:'' The [[$DYNAMIC]] Metacommand allows an array to be re-sized using [[DIM]] and REDIM. | ||
{{CodeStart}} | {{CodeStart}} | ||
'{{Cl|$DYNAMIC}} | '{{Cl|$DYNAMIC}} | ||
{{Cl|INPUT}} "Enter array size: ", size | {{Cl|INPUT}} "Enter array size: ", size | ||
{{Cl|DIM}} Array(size) | {{Cl|DIM}} Array(size) | ||
{{Cl|REDIM}} Array(2 * size) | {{Cl|REDIM}} Array(2 * size) | ||
{{Cl|PRINT}} {{Cl|UBOUND}}(Array) | {{Cl|PRINT}} {{Cl|UBOUND}}(Array) | ||
{{CodeEnd}} | {{CodeEnd}} | ||
''Example 2:'' Shows the difference between REDIM and REDIM [[_PRESERVE]]. | ''Example 2:'' Shows the difference between REDIM and REDIM [[_PRESERVE]]. | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|REDIM}} array(20) | {{Cl|REDIM}} array(20) | ||
array(10) = 24 | array(10) = 24 | ||
Line 47: | Line 47: | ||
{{Cl|REDIM}} array(15) | {{Cl|REDIM}} array(15) | ||
{{Cl|PRINT}} array(10) | {{Cl|PRINT}} array(10) | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{OutputStart}} | {{OutputStart}} | ||
Line 58: | Line 58: | ||
{{PageSeeAlso}} | {{PageSeeAlso}} | ||
* [[Arrays]] | * [[Arrays]] | ||
* [[DIM]], [[SHARED]] | * [[DIM]], [[SHARED]] | ||
* [[_PRESERVE]], [[ERASE]] | * [[_PRESERVE]], [[ERASE]] |
Latest revision as of 00:53, 25 January 2023
A REDIM statement can re-dimension one dynamic(flexible) array or a comma separated list of arrays.
Syntax
- REDIM [[[_PRESERVE]]] [[[SHARED]]] ArrayName[typesuffix] ({max_element|low_element[[[TO]] upper_element, ...]}) [[[AS]] Type]
Description
- Can change the number of elements in an array (the present array data is lost unless _PRESERVE is used).
- Dynamic array elements can also be sized or resized by a program user's entry.
- The _PRESERVE option also allows the element range values to be moved upward or downward in QB64 only!
- Array is the name of the array to be dimensioned or re-dimensioned.
- elements is the number of elements the array should hold. Use the optional TO elements2 to set a range.
- Always use the same array TYPE suffix (AS type) or a new array type with the same name may be created.
- REDIM cannot change $STATIC arrays created with a DIM statement unless the $DYNAMIC Metacommand is used!
- To create a dynamic array use the $DYNAMIC metacommand or use REDIM rather than DIM when first creating the array.
- Use REDIM _PRESERVE to change the range or number of array elements without losing the remaining elements. Data may move up or down to accommodate those boundary changes.
- REDIM _PRESERVE cannot change the number of array dimensions or type!
- Dynamic arrays MUST be REDIMensioned if ERASE or CLEAR are used to clear the arrays as they no longer exist.
- When AS is used to declare the type, use AS to retain that type or it will change to SINGLE!
- NOTE: Many QBasic keyword variable names CAN be used with a STRING suffix($) ONLY! You CANNOT use them without the suffix, use a numerical suffix or use DIM, REDIM, _DEFINE, BYVAL or TYPE variable AS statements!
- Warning! Do not use negative array upper bound index values as OS access or "Out of Memory" errors will occur!
Example 1: The $DYNAMIC Metacommand allows an array to be re-sized using DIM and REDIM.
'$DYNAMIC INPUT "Enter array size: ", size DIM Array(size) REDIM Array(2 * size) PRINT UBOUND(Array) |
Example 2: Shows the difference between REDIM and REDIM _PRESERVE.
REDIM array(20) array(10) = 24 PRINT array(10) REDIM _PRESERVE array(30) PRINT array(10) REDIM array(15) PRINT array(10) |
24 24 0 |
- Explanation: REDIM without _PRESERVE erases the array data and cannot change the number of dimensions.
See also