Tuesday, September 14, 2004

Keeping temporary data using array in VBScript

To declare an array, use the following expression:
dim arrdt()
or
dim arrdt2(20)
for 1-dimension array that can contain 20 variables
or
dim arrdt3(3,10)
for 2-dimension array that can contain 3*10 variables.
You can set an array up to 60 dimensions. If I don't know how big the data will be, so I don't know how big should I set the array size, what should I do? First, just set the array size to 1 (you could set this into 5, or 10 or whatever you want though). Then, set array counter into 0 (because the lower bound of an array is 0).
Dim arramt, arrctr
arramt = 1 
arrctr = 0
After that, resize the array size.
Redim arrdt(arramt)
Everytime you insert a variable into the array, make sure whether the array is big enough to keep the variable. If it is not big enough, make it bigger by resizing the array, remember to use the "preserve" keyword to keep the data inside the array. Also make sure that the number that will be used to resize the array is bigger than the original array size.
if arrctr>=arramt then
   arramt = arramt + 5
   ReDim Preserve arrdt(arramt)	
end if 
And then, set the array content and increase arrctr (add arrctr by one).
arrdt(arrctr) = something_you_want_to_keep
arrctr = arrctr + 1 
After you insert all the variables into the array, you'll know that you have (arrctr-1) variables inside the array.

0 Comments:

Post a Comment

<< Home