Dynamic Arrays – VBScript

by seetaram on September 10, 2011

There are two types of arrays:

  1. Static arrays
  2. 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:

dynamicarray1

Output after resizing the array:

dynamicarray2

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.

preserve

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:

  1. Declare the array with no size – Dim array_name()
  2. 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)

{ 17 comments… read them below or add one }

Rajashree July 12, 2012 at 10:15 am

Well Explained !

Reply

Sunny October 1, 2012 at 5:53 am

nice explanation

Reply

Vineeth K January 11, 2013 at 10:44 am

Clear and precise information..!

Reply

Satya January 13, 2013 at 4:40 pm

Nice Explanation!

Reply

kotimohan February 20, 2013 at 9:57 am

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

Reply

karthick July 10, 2013 at 1:15 pm

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!!!!!!!

Reply

seetaram August 20, 2013 at 9:33 am

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

Reply

karthick July 10, 2013 at 1:17 pm

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……….:):):)

Reply

shankar September 25, 2013 at 6:45 pm

Nice Explanation

Reply

Kishorereddy October 30, 2013 at 10:59 am

Really good explanation, Thanks…

Reply

SierraTangoKilo January 4, 2014 at 4:56 pm

If all examples were so clear. Thanks

Reply

seetaram January 10, 2014 at 6:54 am

why the “IF” …. 🙂

Reply

Azhar Sayed January 30, 2015 at 5:48 am

Nice way of explaining

Reply

ali March 31, 2015 at 6:53 am

Very helpful. Thank you a lot

Reply

Praveen July 2, 2015 at 6:21 pm

Superr buddy….keep it up…….. 🙂

Reply

Kumaran November 20, 2015 at 3:35 pm

Really Nice Explaination.Crystal Clear buddy

Reply

pooja July 28, 2016 at 10:40 am

cool explanation…

Reply

Leave a Comment

Previous post:

Next post:

Do you want to get updates on the recent articles written? Please subscribe to RSS feed or Email