Page 1 of 1

Why I cannot REDIM?

Posted: Mon Apr 26, 2010 6:38 am
by avatar
Please help. I do not understand why I cannot REDIM my array

Code: Select all

Dim GV.l(1,2)
GV(0,0) = 100
GV(0,1) = 200
GV(1,0) = 300
GV(1,1) = 400
ReDim GV(2,2) ; <------------------ ERROR
GV(2,0) = 1100
GV(2,1) = 1200
For Count = 0 To ArraySize(GV())
  Debug Str(GV(Count,0))+","+Str(GV(Count,1))
Next
Error log is :
[13:35:23] Waiting for executable to start...
[13:35:23] Executable type: Windows - x86 (32bit)
[13:35:23] Executable started.
[13:35:23] The Program execution has finished.
[13:36:59] Waiting for executable to start...
[13:36:59] Executable type: Windows - x86 (32bit)
[13:36:59] Executable started.
[13:36:59] [ERROR] test.pbi (Line: 6)
[13:36:59] [ERROR] Only the last dimension of an array can be changed with ReDim.
[13:37:03] The Program was killed.

Re: Why I cannot REDIM?

Posted: Mon Apr 26, 2010 6:43 am
by idle
That would be because your re-dimming the 1st dimension
Only the last dimension of an array can be changed with ReDim.

Re: Why I cannot REDIM?

Posted: Mon Apr 26, 2010 6:57 am
by avatar
Not allow to REDIM the 1st dimension?
What will be the alternative to increase the dimension size?

Re: Why I cannot REDIM?

Posted: Mon Apr 26, 2010 7:18 am
by idle
In the specific case you just need to flip your dim to (2,1) and redim the second dimension
Sorry I can't be of much more help.

Re: Why I cannot REDIM?

Posted: Mon Apr 26, 2010 7:23 am
by avatar
Thanks idle.
You are already very helpful.
May be I consider to better change to use structure list.

Re: Why I cannot REDIM?

Posted: Mon Apr 26, 2010 9:40 am
by Kaeru Gaman
if you don't need to keep the content, just use Dim again.
the content will be erased, but you can use ANY dimensions you like.

Re: Why I cannot REDIM?

Posted: Tue Apr 27, 2010 12:54 am
by charvista
Hi avatar,
This way you can ReDim your Array of type Long, with two dimensions (L2). Somewhat ugly coding, but it works.... and you keep the original GV.l() array.

Hope it helps.

Code: Select all

Procedure zCopyArrayL2(Array FromA.l(2), Array ToA.l(2))
    For i=0 To ArraySize(FromA(),1)
        For j=0 To ArraySize(FromA(),2)
            ToA(i,j)=FromA(i,j)
        Next j
    Next i
EndProcedure    

Dim GV.l(1,1)
GV(0,0) = 100
GV(0,1) = 200
GV(1,0) = 300
GV(1,1) = 400

;ReDim GV(2,2) ; <------------------ ERROR

Dim NewGV.l(2,2)
zCopyArrayL2(GV.l(),NewGV.l())
Dim GV.l(2,2)
zCopyArrayL2(NewGV.l(),GV.l())
Dim NewGV(0,0) ; reduce to a minimum to save memory

GV(2,0) = 1100
GV(2,1) = 1200
For Count = 0 To ArraySize(GV())
  Debug Str(GV(Count,0))+","+Str(GV(Count,1))
Next