I really can't remember how it is done.
We all know that to make a value be static to a function or sub we use the STATIC command:
Code:
SUB dummy
STATIC a
a = a + 1
PRINT a
END SUB
If we call this sub 5 times it will print 1, 2, 3, 4 and 5.
What about arrays? I have this:
Code:
SUB dummy2
DIM a(7)
...
END SUB
How do I make that array static? I get a duplicate definition error when I try to add a STATIC a() to the code.
Any clues?
DECALARE SUB foo() STATIC
-Just Guessing
Nope. That would make STATIC every variable that is used inside the SUB, and will cause shared variables to lose the value they have outside the sub. I just want that a() to be STATIC.
The problem is, the second time you call the sub, the array has already been DIMed, thus you get an error. You could use REDIM, but that would destroy the contents of the array (unless you have qb 7.1 and use REDIM PRESERVE).
The solution is to use a static flag. Example: (tested and works in qb 4.5)
Code:
DEFINT A-Z
' Pass an index and value to change an element in asdf's static
' array, or 0, 0 to print out the contents of the array.
DECLARE SUB asdf (i%, v%)
CLS
asdf 1, 3
asdf 3, 5
asdf 2, 5345
asdf 0, 0
END
SUB asdf (i, v)
STATIC aHasAlreadyBeenDIMed
STATIC a() AS INTEGER
IF aHasAlreadyBeenDIMed = 0 THEN
DIM a(1 TO 10) AS INTEGER
aHasAlreadyBeenDIMed = 1
END IF
IF i = 0 THEN
FOR i = 1 TO 10
PRINT a(i);
NEXT i
PRINT
ELSE
a(i) = v
END IF
END SUB
Personally, I find it much easier to use shared arrays instead, and DIM them at program initialization - then you wouldn't need a flag at all.
-
Yeah, I would SHARE them, but the program is acting strange 'cause it takes up too much memory, so SHARED array values are lost and corrupted :S
I'll try this sollution. Thanks! (BTW, why QB help doesn't mention this issue?)
EDITED: Okay, I got it working. Clara will be happy. BTW, Lachie, now's your turn
I'll work on your terminal thingie right now.