Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Runtime error 9 when Preserving a UDT Array

2 views
Skip to first unread message

wxforecaster

unread,
Dec 31, 2009, 12:45:19 PM12/31/09
to
I have two user-defined type structures, with one embedded inside
another due to the number of elements required at the variable level
(far more elements than shown in the example below).

For example:

Public Type DEF
a as string
b as long
c as single
d as byte
End Type

Public Type ABC
name as string
index as long
itemdata as DEF()
End Type


I have no problem initally dimming the arrays and populating them. The
specs call for a maximum array size of ABC of 1 to 25, and DEF of 1 to
720.
Thus I begin with the follolowing
Dim udtABC() as ABC, i as long
Redim udtABC(1 to 25)
For i = 1 to 25
ReDim udtABC(i).itemdata(1 to 720)
Next i

The program then fills all the arrays and UDT variables with data,
quite efficiently. The problem is that there are far more variables
than shown here, so the ABC() array could be on the order of 20-30MB
by the time its filled with data for rendering. Thus, since the 25
element max on ABC() is rarely reached (as is the 720 max on DEF), I
would like to trim off the excess array elements.

Both of the following examples fail with a run-time error 9 and I
can't figure out why.
Dim defCount as Long, abcCount as Long
defCount = 600
ReDim Preserve udtABC(i).itemdata(defCount)

abcCount = 20
ReDim Preserve udtABC(abcCount)


Evan

Nobody

unread,
Dec 31, 2009, 1:32:29 PM12/31/09
to
"wxforecaster" <wxfore...@kc.rr.com> wrote in message
news:34759c38-73e4-4a1c...@a15g2000yqm.googlegroups.com...

They fail because you are specifying a lower bound in the declaration, and
using the implied lower bound in ReDim Preserve.


Nobody

unread,
Dec 31, 2009, 1:36:11 PM12/31/09
to
Also, it's not required that all DEF inside ABC have the same size. For
example, ABC(1) could have DEF(100) while ABC(2) has DEF(200).


Larry Serflaten

unread,
Dec 31, 2009, 2:05:49 PM12/31/09
to

"wxforecaster" <wxfore...@kc.rr.com> wrote

> I have two user-defined type structures, with one embedded inside
> another due to the number of elements required at the variable level
> (far more elements than shown in the example below).

I seem to recall that is a recipe for disater. Something about excessive
memory requirements...

> Thus I begin with the follolowing
> Dim udtABC() as ABC, i as long
> Redim udtABC(1 to 25)
> For i = 1 to 25
> ReDim udtABC(i).itemdata(1 to 720)
> Next i

Note you use a lower bound of 1 (0 is the default, correct?)


> Both of the following examples fail with a run-time error 9 and I
> can't figure out why.
> Dim defCount as Long, abcCount as Long
> defCount = 600
> ReDim Preserve udtABC(i).itemdata(defCount)
>
> abcCount = 20
> ReDim Preserve udtABC(abcCount)
>

You let VB supply the lower bound (of 0) which is out of the
range you initially declared. The same error can be shown by
this example:

Dim A() As Long
ReDim A(1 To 5)
ReDim Preserve A(2)

HTH
LFS


wxforecaster

unread,
Dec 31, 2009, 5:48:05 PM12/31/09
to
The first response was the "duh" moment. The base 1 array is
intentional here, and indeed it crashed because the Redim Preserve did
not also state that the base was 1 and not the default 0.

Thanks "nobody" and Larry.

I would have changed the Option Base, except there are standard 0-
based arrays used in the software. In this case the two 1-base arrays
save a lot of extraneous calculations because the array indices are
explicitly stored in the binary data we're reading in.

Evan

"All your base are belong to us"

0 new messages