Thursday, January 3, 2019


Types of Array in vb script


There are 2 types of arrays in vb script.
  • Static(Fixed Size)
  • Dynamic
Static Arrays
Static array can contain fixed number of elements in array.
Example -
dim a (10) - This static array will contain only 11 elements.
If you want to increase/decrease the size of array, it will not be possible.

Dynamic Arrays

In dynamic array we can change the size of array.

Dim b() - This is how we define dynamic array.

To use dynamic array we must define the size.

Redim b(5) - Array b redefined with size of 5 means it can contain 6 elements.

Array containing 6 elements

Now if you want to increase the size of elements in array you can use below statment

redim b(7) - In this case array can contain 8 elements but previous elements will be erased.



To preserve the contents of the array,  we can use
Redim preserve b(7)



When we reduce the size of dynamic array , data of the array is lost.

For example if we have a array b

redim preserve b(8)

and we execute below statement

redim preserve b(3) 
Then we will not be able to access the elements at  the index 4 onwards.

Erasing elements in the Array
We can use erase statement to remove the data from fixed size array. Please note that memory is still occupied by the array after erasing in fixed size array.

'Erasing Fixed size array
Dim a(2)
a(0)=11
Erase a

We can use erase statement to release the memory occupied by the dynamic array.
'Erasing Dynamic Array
Dim da()
ReDim da(2)
da(0) = 12
Erase da

Please note that we can have multi-dimensional arrays as well.Dim twodim(5, 10)
In above array, there will be 6 rows and 11 clolumns


0 comments:

Post a Comment

Blog Archive

Translate

Popular Posts

Total Pageviews

Blog Archive