redim preserve myArray(i)

G

Guest

Is it correct to think that after reducing the populated array's size from
say, 10 to 5 with
redim preserve myArray(i)
an attempt to access an element above the fifth does not cause a
compillation error "array out of script", but returns whatever heppened to
be written in that memory address (in particular it might return the correct
values of those elements before re-dimentioning)?
This seems to be the case in my code, yet I wanted to make sure that this is
not the result of some other side effect
 
B

Bob Barrows

aa said:
Is it correct to think that after reducing the populated array's size
from say, 10 to 5 with
redim preserve myArray(i)
an attempt to access an element above the fifth does not cause a
compillation error "array out of script", but returns whatever
heppened to be written in that memory address (in particular it might
return the correct values of those elements before re-dimentioning)?
This seems to be the case in my code, yet I wanted to make sure that
this is not the result of some other side effect

Nope. As expected, this code raises the "Subscript out of range: '5'" error
on my machine:

<%
dim ar(),i
redim ar(10)
for i=0 to 10
ar(i)=1
next
redim preserve ar(4)
Response.Write ar(5)
Response.End
%>

If you are using "on error resume next", and you attempt to redim an array
that cannot be redim'ed (the only redimmable arrays are those declared with
the syntax shown in my dim statement above), you may not receive the error
message, and the array will be unaffected by the redim statement. That is
what happens when I change my code to this:

<%
on error resume next
dim ar(10), i
for i=0 to 10
ar(i)=i
next
redim preserve ar(4)
for i = 0 to 10
Response.Write ar(i) & "<BR>"
next
Response.End
%>

Bob Barrows
 
G

Guest

Thans, Bob, that's exactly what I wanted to know.
My code redims an array up and down and thought it does not produce an
error, sometimes the final result is incorrect.
So I have an error elsewhere
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top