COMMON SHARE an array?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
deems
Newbie
Posts: 2
Joined: Sun May 03, 2015 7:03 pm

COMMON SHARE an array?

Post 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?
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: COMMON SHARE an array?

Post 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.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
deems
Newbie
Posts: 2
Joined: Sun May 03, 2015 7:03 pm

Re: COMMON SHARE an array?

Post 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.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: COMMON SHARE an array?

Post by burger2227 »

Yes, you only need to put one COMMON SHARED list per module with commas between all variables listed.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply