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

Related Posts:

  • How to shutdown a computer in VBScript? How to shutdown a computer in VBScript? 'Warning - if you run this script, your computer will shut down'Here you can specify name of the computer you want to  shut-down.....'. means local computerstrComputer = ".… Read More
  • Dynamic Arrays in VBScript example Dynamic Arrays in VBScript example In Vb script we can define the dynamic array in vb script as mentioned below.Size of dynamic array changes at run time. Example -Dim a () - declared dynamic array aTo use dynami… Read More
  • How to Restart a machine in vbscript? How to Restart a machine in vbscript? 'Warning - if you run this script, your computer will reboot/restart'Here you can specify name of computer you want to restart.....'. means local computerstrComputer = "."'create W… Read More
  • How to list all files in cookies in vbscript? How to list all files in cookies in vbscript? Cookie is a file stored by the web server on web client machine.If you want to find out the cookies in your machine you can use below program.Const COOKIES = &H21&S… Read More
  • How to read the environment variables in your system in vbscript? How to read the environment variables in your system in vbscript? You can read the system variables using below program        'Create the WMI object strComputer = "." Set objWMIService = GetObject… Read More

0 comments:

Post a Comment

Blog Archive

Translate

Popular Posts

Total Pageviews

150,585

Blog Archive