At the first line, the array is declared as static (fixed-sized).
So in the scope [Do...Loop], Redim defines a new local dynamic array, and the shared array is not resized.
Solution:
Define first a dynamic array:
ReDim shared as String fileread(1 to 1)
or
Dim shared as String fileread()
ReDim fileread(1 to 1)
Remark: If you compile your initial code with option -exx, a runtime error must appear in the loop.
Because at each loop iteration:
- Before instruction 'Redim', the program accesses the shared array (unresized)
- After instruction 'Redim', the program accesses the local array (increasingly resized at each loop)
Example with
Dim shared as String fileread(1 to 1):
Code: Select all
Dim shared as String fileread(1 to 1)
Dim as Long cnt=0
print " ubound(fileread)"
print "before redim", "after redim"
do until cnt=10
cnt += 1
print ubound(fileread),
redim preserve fileread(cnt+1)
print ubound(fileread)
loop
sleep
Code: Select all
ubound(fileread)
before redim after redim
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
Example with
Redim shared as String fileread(1 to 1):
Code: Select all
Redim shared as String fileread(1 to 1)
Dim as Long cnt=0
print " ubound(fileread)"
print "before redim", "after redim"
do until cnt=10
cnt += 1
print ubound(fileread),
redim preserve fileread(cnt+1)
print ubound(fileread)
loop
sleep
Code: Select all
ubound(fileread)
before redim after redim
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11