ReDim - VBScript
ReDim Statement
Overview
The ReDim statement in VBScript dynamically redimensions an array to a new size or shape. It allows you to reallocate memory for an array during runtime, making it flexible and dynamic.
Syntax
ReDim [{|ArrayName|((|,|Dimensions)*)|(Subscripts)}] [Preserve]
Options/Flags
| Option | Description |
|—|—|
| Preserve | Preserves the existing array values during redimensioning. |
Examples
Simple Redimensioning:
Dim myArray(1 To 10)
ReDim myArray(1 To 20)
Multidimensional Array Redimensioning:
Dim myMatrix(1 To 3, 1 To 4)
ReDim Preserve myMatrix(1 To 5, 1 To 6)
Redimensioning with Subscripts:
Dim myList: Set myList = CreateObject("Scripting.Dictionary")
myList.Add "Key1", "Value1"
ReDim Preserve myList("Key2", "Value2")
Common Issues
- Incorrect Dimensions: Ensure you specify valid dimensions for the array.
- Out of Memory: If the system runs out of memory during redimensioning, an error will occur.
- Data Loss: Using the Preserve flag is crucial to prevent data loss during redimensioning.
Integration
The ReDim statement works seamlessly with other VBScript commands, such as:
- Option Base to specify the base index for arrays.
- For Each…Next to loop through the elements of an array.
- Array() to create and initialize arrays.
Related Commands
- Erase: Clears the contents of an array.
- LBound: Gets the lower bound of an array.
- UBound: Gets the upper bound of an array.