Saturday, July 31, 2010

VBSCRIPT > REDIM PRESERVE clear example


VBSCRIPT > REDIM PRESERVE example

THROWS - subscript out of error
dim a()
i=0
for i =0 to 5
a(i)=i ' subscript out of error at this line
next
----------------------
dim a()
i=0
for i=0 to 5
REDIM a(i)
a(i)=i
next

'output
for i=0 to ubound(a)
msgbox a(i)
next

'OUTPUT : 5
' Where are the values 0,1,2,3,4 ??? strange ??
' these values have been overwritten each time and only last updated value is stored in a(i)

'TO PRESERVE Previously stored value in array, we use -PRESERVE keyword as follows
dim a()
i=0
for i=0 to 5
REDIM PRESERVE a(i)
a(i)= i
Next

'to retrieve output
for i=0 to ubound(a)
msgbox a(i)
next
'OUTPUT : 0,1,2,3,5

No comments:

Post a Comment