Annoying illegal redim error message

Hi

I keep getting the Illegal redim error. I know that the cause could be that I am redimming a fixed array, but that is not the case.

Here is my code:

Dim MatchArr() As String

Redim MatchArr(0)

while not doc is nothing

top = Ubound(MatchArr)

IncNo = result.GetValue(1)

MonthN  = Cint(result.GetValue(3))

YearN  = Cint(result.GetValue(4))

MatchArr(top) = Cstr(ticketmatchCount)

Redim Preserve MatchArr(top + 1) ===> Error

numRows = numRows + 1

Wend

I dont know where to look. Why am I getting this error message?

Regards

Subject: Annoying illegal redim error message

It appears that Top will never be incremented - maybe this is an issue (I’ve never redimed an array to it’s curent dimensions)

top = ubount(MatchArr) = 0

redim preserve MatchArr(top + 1) results in “top = ubount(MatchArr) = 0”

if it won’t cause issues with other things in the code, this is a good place to use Option Base 1

wait… I just looked at your code again - the first redim is wrong - the first redim should be:

Redim MatchArr(1)

you still need to consider option base though.

Subject: RE: Annoying illegal redim error message

Huh? MatchArr starts off with a Ubound of 0 (first redim). In the while loop, top is set to the current Ubound, then the array is redimmed to one greater than top. That gets you to 1 on the first loop. Next time around, top is set to 1 and the array is redimmed at 2 (1+1). Option Base has nothing to do with it, and is generally a bad idea unless you use it everywhere and make sure that you add it to the comments of every sub and function you create as well as to the Options area – it counts as “unexpected behaviour”. Personally, I’d use a List rather than an array if I know it’s going to have to redimmed a thousand and twenty-three times; it’s a lot less expensive than continual redim preserves.

Subject: RE: Annoying illegal redim error message

Hi Stan

Thank you for responding.

In my case I, indeed, need to fill the array with almost 20000 entries. Is it the wise to use a list instead an array? If I use redim for each entry does that affect the performance a lot? I have seen that the filling of my arra takes more than 40 minutes. And thats a lot for the server on which it should run.

Regards

Subject: RE: Annoying illegal redim error message

Yes, it would affect performance. Every Redim Preserve essentially creates a whole new copy of the array in a new memory location, with one extra element allotted. Using a list would be a lot faster:

Dim MatchList List As String

top = 0

while not doc is nothing

IncNo = result.GetValue(1)

MonthN = Cint(result.GetValue(3))

YearN = Cint(result.GetValue(4))

MatchArr(Cstr(top)) = Cstr(ticketmatchCount)

top = top + 1

numRows = numRows + 1

Wend

In the end, you are left with a list having list tags like “0”, “1”, “2”, etc. If you still need an array, you can dump the list into one:

(Assume “Dim MatchArray() As String” is still decalred earlier)

Redim MatchArray(top - 1)

Forall entry In MatchList

MatchArray(Cint(ListTag(entry)) = entry

End Forall

That should go a heck of a lot faster than the method you’re using now.

Subject: RE: Annoying illegal redim error message

teensy typo above for anyone using this.

2nd line from bottom:

MatchArray(Cint(ListTag(entry)) = entry

needs a final additional closing bracket (i.e. 3 brackets at the end)

TextList(Cint(Listtag(entry)))

Subject: Annoying illegal redim error message

Is “top” dimmed as an Integer or a Long? (A Variant won’t work, since LS cannot determine whether or not the value would be a legal array bound.)

Subject: RE: Annoying illegal redim error message

Hi

top is dimmed as integer.

Regards