Qbasicnews.com
May 26, 2020, 06:45:27 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: STATIC and arrays  (Read 1393 times)
na_th_an
*/-\*
*****
Posts: 8244



WWW
« on: November 12, 2003, 04:26:17 PM »

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?
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
red_Marvin
Na_th_an
*****
Posts: 1509



WWW
« Reply #1 on: November 12, 2003, 04:27:59 PM »

DECALARE SUB foo() STATIC

-Just Guessing
Logged

/post]
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #2 on: November 12, 2003, 04:29:32 PM »

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.
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Sterling Christensen
Na_th_an
*****
Posts: 1328


« Reply #3 on: November 12, 2003, 04:55:31 PM »

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.

-
Logged
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #4 on: November 12, 2003, 05:19:19 PM »

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 Wink I'll work on your terminal thingie right now.
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!