We help IT Professionals succeed at work.

What's wrong with this simple ReDim Preserve statement?

Medium Priority
1,603 Views
Last Modified: 2008-01-25
Why do I get the following error when I run the attached code?

Microsoft VBScript runtime error '800a0009'

Subscript out of range

/sign/usvp/SendApproval.asp, line 5

<%
Dim arTest
ReDim arTest(0,2)
arTest(0,2) = "test"
ReDim Preserve arTest(4,2)
arTest(1,2) = "test2"
Response.End()
%>

Open in new window

Comment
Watch Question

Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
you can only resize the last dimension of an multi-dimensional array.

Author

Commented:
So then I need to switch it up to arTest(0)(2) or something instead?

Author

Commented:
This works fine by the way - but I lose the previous data of course:
<% 
Dim arTest
redim arTest(0,2)
arTest(0,2) = "test"
ReDim arTest(1,2)
arTest(1,2) = "test2"
response.End
%>

Open in new window

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
what about this:
<%
Dim arTest
ReDim arTest(0 to 2, 0 to 0)
arTest(2,0) = "test"
ReDim Preserve arTest(0 to 2,0 to 4)
arTest(2,1) = "test2"
Response.End()
%>

Open in new window

CERTIFIED EXPERT

Commented:
I keep getting caught when I forget to think.... happens all the time!

you must switch your dimensions around as angel has shown

instead of  asTest(x,y)    need to use arTest (y,x)     because only the second dimension can be redimed.

redim arTest(5,20)
for x=0 to 20
   for y=1 to 5
     arTest(y,x)=999
   next y
next x

redim preserve arTest(5,25)

Author

Commented:
Ahhhhhh - yes, sorry it's been four years since I've worked in VBScript and I totally forgot that you put columns before rows (cols,rows) in an array.  Boy - I'm rusty! :P  thanks all!
CERTIFIED EXPERT

Commented:
no. its just when you want to redim that you need to do this,

Explore More ContentExplore courses, solutions, and other research materials related to this topic.