Dim


Syntax
Dim name.<type>(<expression>, [<expression>], ...) 
Description
Dim is used to 'size' the new arrays. An array in PureBasic can be of any types, including structured, and user defined types. Once an array is defined it can be resized with ReDim. Arrays are dynamically allocated which means than a variable or an expression can be used to size them.

When you define a new array, please note that it will have one more element than you used as parameter, because the numbering of the elements in PureBasic (like in other BASIC's) starts at element 0. For example when you define Dim(10) the array will have 11 elements, elements 0 to 10. This behaviour is different for static arrays in structures.

The new arrays are always locals, which means than Global or Shared commands have to be used if an array declared in the main source need to be used in procedures. It is also possible to pass an array as parameter to a procedure.

To delete the content of an array and release its used memory during program flow, call Dim with array name and 0 elements.

Note: Array bound checking is only done when the runtime Debugger is enabled.

Example:

  Dim MyArray.l(41)
  MyArray(0) = 1
  MyArray(1) = 2 

Example: Multidimensional array

  Dim MultiArray.b(NbColumns, NbLines)
  MultiArray(10, 20) = 10
  MultiArray(20, 30) = 20

Syntax
ReDim name.<type>(<expression>, [<expression>], ...) 
Description
ReDim is used to 'resize' an already declared array while preserving its content. The new size can be larger or smaller, but the number of dimension of the array can not be changed.

If ReDim is used with a multi-dimension array, only its last dimension can be changed.

Example:

  Dim MyArray.l(1) ; We have 2 elements
  MyArray(0) = 1
  MyArray(1) = 2
  
  ReDim MyArray(4) ; Now we want 5 elements
  MyArray(2) = 3
  
  For k = 0 To 2
    Debug MyArray(k)
  Next