Page 1 of 1

COMMON SHARE an array?

Posted: Mon May 04, 2015 3:06 am
by deems
I've been working on a small game project for a year or two, in good ol QB45. Just recently I went over the 64kb limit and had to start dividing up my code into modules. Here's a rough summary of the problem:

Code: Select all

'FILE1.BAS
DIM SHARED INV$(20)
DIM SHARED FLAG$(25)

'FILE2.BAS
COMMON SHARED INV$()
COMMON SHARED FLAG$()
LET FLAG$(0) = ""
PRINT UBOUND(INV$)
The error comes with file2- both the LET and UBOUND return 'subscript out of range,' presumably because COMMON SHARED doesn't dimension a variable. I've never messed with modular programming before, can someone help point me in the right direction?

Re: COMMON SHARE an array?

Posted: Mon May 04, 2015 12:20 pm
by burger2227
SHARED variables are only shared inside a module, a COMMON SHARED list shares values between CHAIN'ed modules.

You can just DIM arrays(30) and then include them in a COMMON SHARED list. Each module must have the list of variables in the identical order!

I've found that you can avoid memory problems by using SUB and FUNCTION calls especially when working with STRINGs and PRINTs.

Re: COMMON SHARE an array?

Posted: Mon May 04, 2015 1:59 pm
by deems
Thanks for the quick response! I think I'm getting it-- COMMON SHARED has to be included in both BAS files, and in the case of arrays, written out before DIM SHARED in the main module. This:

Code: Select all

'file1.bas
COMMON SHARED INV$()
DIM SHARED INV$(20)

'file2.bas
COMMON SHARED INV$()
...appears to be working just fine.

Re: COMMON SHARE an array?

Posted: Mon May 04, 2015 3:19 pm
by burger2227
Yes, you only need to put one COMMON SHARED list per module with commas between all variables listed.