Assigning Array Size

When assigning the size of an array in the below example:

Dim j as Integer j=5 Dim a(j) as Integer

I receive an error that The Size of an Array Must Be a Constant or a Number.

if I do something like:

Dim a(5) as Integer

it works fine. Not sure why the first example generates error.

j is a variable and not constant - it is exactly that in the second case

Suppose you passed J in to a method that had that code in it
one time j could be 5, one time it could be 9 the next 890 (you get the idea)

However there is a way to achieve what you want

Dim j as Integer
j=5
Dim a(-1) as Integer
redim a(j)

Thank you Norm!

that worked a treat :slight_smile:

[quote=127380:@Norman Palardy]j is a variable and not constant - it is exactly that in the second case

Suppose you passed J in to a method that had that code in it
one time j could be 5, one time it could be 9 the next 890 (you get the idea)

However there is a way to achieve what you want

Dim j as Integer j=5 Dim a(-1) as Integer redim a(j) [/quote]

Thank you for that tip, Norman. I bumped into the same problem a couple weeks ago and had simply overdimensioned the array. This is much better.