VB6 ReDim -- error 9 -- where am I going wrong?

This should be simple, but line 1050 is reporting error 9, subscript out of bounds.

Laugh at me if you must ... but where is my error?

Dim marBarControlsEnabled() As Boolean 
1040     If Not IsArray(marBarControlsEnabled) Then GoSub InitArray
1050     If UBound(marBarControlsEnabled, 1) < CommandBars1.Count Then GoSub InitArray
... now use the array
1200 InitArray:
    Dim iMaxBar As Integer, iMaxControl As Integer
'code to figure out the maxima ...
1270     ReDim Preserve marBarControlsEnabled(iMaxBar, iMaxControl)
1280     Return

Open in new window

LVL 32
Daniel WilsonAsked:
Who is Participating?
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
you can only change 1 (the last) dimension of an array in VB6.

Dim marBarControlsEnabled() As Boolean
is a 1-dimension array, so you cannot do this:
ReDim Preserve marBarControlsEnabled(iMaxBar, iMaxControl)

How is your array declared ? marBarControlsEnabled ?

If it's a one-dimension array, try this: If UBound(marBarControlsEnabled, 0)
Daniel WilsonAuthor Commented:
I need it to be 2-dimensional.

But if I start with

Dim marBarControlsEnabled(2,200) As Boolean

the Redim preserve won't compile.  It's already been dimensioned.

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Daniel WilsonAuthor Commented:
I think I found it ... http://www.xtremevbtalk.com/showthread.php?t=82476

testing ...
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you will need some other method than array.
You can only ReDim an array whose dimensions are not defined.

Dim marBarControlsEnabled() As Boolean

Did you initially create the array with any dimensions?
It is possible to declare an array without specifying the dimensions.  If this is what you have done, then attempting to get a UBound on an undimensional array is going to error out.

A trick that I have done before to create a legal array, but FLAG it as "uninitiallized" is to dimensionalize it with -1.  For example, if you have a ONE dimensional array, I declare it like this...

Dim MyArray(-1 to -1) as Integer

Then I can test the array for initilization with UBound...

if UBound( MyArray, 1 ) < 0 then
   ReDim MyArray(1 to Max)
  ReDim Preserve MyArray(1 to Max)

The concept can be extended to multi-dimensions, just start with each diminsion from -1 to -1.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.