What is the use of 'Preserve' keyword in VB Script?

The Preserve keyword is very important when using ReDim. 
For example, that you create a dynamic array, specifying its storage space by using ReDim, fill it with data, and then later decide to make it larger so you can fill it with more information without losing your original data. In the following example, ReDim sets the initial size of the dynamic array to 25. A subsequent ReDim statement resizes the array to 30, but uses the Preserve keyword to preserve the contents of the array as the resizing takes place.

Syntax:

   ReDim MyArray(25)
       . . .
        ...
   ReDim Preserve MyArray(30)

There is no limit to the number of times you can resize a dynamic array.

Example:

    Dim car()
    Redim car(1)    
    car(0)="volvo"
    car(1)="bmw"
    Redim preserve car(2)
    car(2)="benz"
    msgbox car(1)
    msgbox car(2)

Output
    bmw
    benz



To receive daily posted JOBS & Interview Questions
Just enter your email address below and click 'Submit'
Enter your email address:

Make sure to activate your subscription by clicking on the activation link sent to your email