There are two types of arrays:
- Static arrays
- Dynamic arrays
Static array has to be declared with the number of elements that the array can hold (i.e. arr(5)). If we don’t know the number of elements to be stored in the array, then the dynamic array can be used.
Syntax:
Dim arr()
ReDim keyword should be then used to declare the number of elements that the array can hold.
Syntax:
Dim arr()
ReDim arr(5)
Example:
Dim arr() ReDim arr(5) arr(0)=10 arr(1)=15 arr(2)=20 arr(3)=25 arr(4)=30 arr(5)=35 msg="" For i=0 to 5 msg=msg&"Arr("&i&") : "&arr(i)&vbnewline Next msgbox msg ReDim arr(6) arr(6)=40 msg="" For i=0 to 6 msg=msg&"Arr("&i&") : "&arr(i)&vbnewline Next msgbox msg
In the above example, array is initially sized with five elements. Array elements with indices 0 to 5 are initialized and displayed using for loop. The array is resized with ReDim keyword and then the added array element is initialized with the statement “arr(6)=40”.
Let us execute the script and have a look at the output.
Output before resizing the array:
Output after resizing the array:
If we see both of these images, definitely a question pops up in our mind. Why in the second output Arr(0) to Arr(5) are blank? Why Arr(6) is only having a value.
To overcome this problem, we need to use Preserve keyword
We will use the Preserve keyword with the ReDim in the above example.
Dim arr() ReDim arr(5) arr(0)=10 arr(1)=15 arr(2)=20 arr(3)=25 arr(4)=30 arr(5)=35 msg="" For i=0 to 5 msg=msg&"Arr("&i&") : "&arr(i)&vbnewline Next msgbox msg ReDim Preserve arr(6) arr(6)=40 msg="" For i=0 to 6 msg=msg&"Arr("&i&") : "&arr(i)&vbnewline Next msgbox msg
Let us execute the new script with Preserve keyword and see the output.
Output shows the array element values starting from Arr(0) to Arr(6). Preserve keyword preserves the values of the array elements before ReDim statement. Hence, if we resize the array with ReDim and add new elements does not erase the earlier values of the array.
Follow the steps below to use the dynamic arrays:
- Declare the array with no size – Dim array_name()
- Re-declare the size of the array with ReDim & Preserve – ReDim Preserve array_name(number of elements)
Example:
Dim arr()
ReDim arr(5)
{assign values}
ReDim Preserve arr(10)
Well Explained !
nice explanation
Clear and precise information..!
Nice Explanation!
Unable to retrieve the previous values by using the preserve function, can you support me I need total values to display by using the function;
Ex:
Dim arr()
Redim arr(8)
arr(0)=18
arr(1)=28
arr(2)=38
arr(3)=48
arr(4)=58
arr(5)=68
arr(6)=78
arr(7)=88
arr(8)=98
msg=””
For i=0 to 8
msg=msg&”arr(“&i&”) : “&arr(i)&vbnewline
Next
msgbox msg
Redim arr(12)
arr(12)=128
msg=””
For i=0 to 12
msg=msg&”arr(“&i&”) : “&arr(i)&vbnewline
Next
msgbox msg
Redim Preserve arr(15)
arr(15)=158
msg=””
For i=0 to 15
msg=msg&”arr(“&i&”) : “&arr(i)&vbnewline
Next
msgbox msg
U missed the Preserve keyword in the first declaration.
msg=msg&”Arr(“&i&”) : “&arr(i)&vbnewline
i dont understand this line use of so many ampersand made me bewildered. please help me to understand.. thanks a tonne!!!!!!!
Ampersand is used to just concatenate two different strings. Here in this line of code, every time string msg is concatenated with whatever there inside the double quote as well as the array element
i dont understand formatting of output… if anyone helps me it wil be grateful…… thanks in advance….. while typing this comment i found the font type is good…. what is the name of font if u know please reply for that also……….:):):)
Nice Explanation
Really good explanation, Thanks…
If all examples were so clear. Thanks
why the “IF” …. 🙂
Nice way of explaining
Very helpful. Thank you a lot
Superr buddy….keep it up…….. 🙂
Really Nice Explaination.Crystal Clear buddy
cool explanation…
Awsome, very easily understandable 🙂
Thank yo Ashok
What if I need to shrink the array, rather than expand it? Would Redim (+ Preserve) work for a dimension smaller than the current UBound?
I think Redim should work. Currently I am not working on VB script but I remember using Redim.