How to redim array in a procedure

Just starting out? Need help? Post your questions and find answers here.
Goos E
New User
New User
Posts: 6
Joined: Sun Mar 15, 2009 2:55 pm
Location: Netherlands

How to redim array in a procedure

Post by Goos E »

Hello,

I am trying to write a procedure witch inits a program. I am reading a array from a textfile. I want to use this array global in my program. my question is How to redim a array in a procedure while i use it global. With the next example i get a error named "Redim can't be used on a non-declared array:bmkust"

Code: Select all

procedure init()
do
 a$="1000"
 tel=tel+2
 redim bmkust(tel)
 bmkustx(tel)=val(a$)
until a$="1000"
endprocedure
global dim bmkust(1)
init()
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Purebasic is a one-pass compiler... as a consequence you must position the Global statement before making first use of the array.

The same is true for any variables. If you use the global statement after the same variable appears in a function, PB will assume that the function variable is a local variable.

In short, put the Global statement at the top of your source.

Code: Select all

Global Dim bmkust(1) 

Procedure init() 
Repeat
 a$="1000" 
 tel=tel+2 
 ReDim bmkust(tel) 
 bmkust(tel)=Val(a$) 
Until a$="1000" 
EndProcedure 
init() 
(I took the liberty of removing the 'Do' statement which does not exist in Purebasic and of correcting a mis-spelled variable.)
I may look like a mule, but I'm not a complete ass.
Goos E
New User
New User
Posts: 6
Joined: Sun Mar 15, 2009 2:55 pm
Location: Netherlands

Post by Goos E »

Thank you very much that helped.
Post Reply