how to share Common declare between modules

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
gablea
Newbie
Posts: 2
Joined: Thu May 13, 2010 8:08 am

how to share Common declare between modules

Post by gablea »

Hi everyone,

I have just started to develop once again in qbasic and for the love of everything I can not remember how to share a variable between modules

Before the main function runs I have declared

Common shared TerminalNumber as string

But I can not seem to access it from another function

Example

Code: Select all

Public sub PrintTerminalNumber
  Print "Terminal Number :" + TerminalNumber
End sub
I end up with Terminal Number :

How do I allow the printTerminalNumber sub access to the TerminalNumber?

I even have tried to declare it like

Code: Select all

public sub PrintTermialNumber (byval termID string)
  Print "Terminal Number :" + termID
End sub
And then

Code: Select all

PrintTerminalNumber (TerminalNumber)
but I still end up with Terminal Number :

I will say this now I have been using VB6 for meany years and I think i have forgot most of my QBasic skills I I do appreciate any help or example
Code on this

Thanks
Andrew
MikeHawk
Veteran
Posts: 61
Joined: Sun Jul 08, 2018 11:23 am

Re: how to share Common declare between modules

Post by MikeHawk »

Wait, does your code actually work in QBasic or QuickBASIC? QB doesn't have the PUBLIC/PRIVATE declaration (everything is PUBLIC) and all variables are passed BYREF (I don't think you can explicitly use BYVAL unless it's for CALL ABSOLUTE or a non-BASIC procedure.) I'm not sure the code you wrote would execute.

COMMON is typically used when sharing variables between modules (more than one source file, more than one compiled program, or when using libraries;) for instance, you have 1.BAS and 2.BAS. In 1.BAS, you declare a variable using COMMON and then, CHAIN to 2.BAS later on. In 2.BAS, you also declare that variable using COMMON and QBasic should carry the data from one program to the other (only the type and the order in which variables are declared with COMMON is important.) It works with interpreted source code in the IDE, or with BRUN45-dependent EXEs.

Since you're writing a one-file program, I don't think you need to use COMMON. Just go straight for the DIM SHARED <var> AS <type> in your main module and have the variable shared among all routines... that being said COMMON SHARED is still technically valid and the following code works as intended in QuickBASIC 4.5:

Code: Select all

DECLARE SUB PrintTerminalNumber()

COMMON SHARED TerminalNumber AS STRING

TerminalNumber = "1234"
PrintTerminalNumber

SUB PrintTerminalNumber
  PRINT "Terminal Number :"; TerminalNumber
END SUB
Typical use would be:

Code: Select all

' 1.BAS
COMMON myName AS STRING
INPUT "What's your name"; myName
CHAIN "2.BAS"

Code: Select all

' 2.BAS
COMMON someName AS STRING
PRINT "So, your name is "; someName
Or, with SHARED:

Code: Select all

' 1.BAS
COMMON SHARED myName AS STRING
INPUT "What's your name"; myName
CHAIN "2.BAS"

Code: Select all

' 2.BAS
DECLARE SUB MyRoutine()

COMMON SHARED someName AS STRING

MyRoutine

SUB MyRoutine
  PRINT "So, your name is "; someName
END SUB
MikeHawk
Veteran
Posts: 61
Joined: Sun Jul 08, 2018 11:23 am

Re: how to share Common declare between modules

Post by MikeHawk »

Okay, there's another way to share a variable only between certain routines: define the variable as usual in the main module, then use SHARED within the routine:

Code: Select all

DECLARE SUB test1 ()
DECLARE SUB test2 ()

DIM something AS STRING

something = "anything, really"

test1
test2
END

SUB test1
  SHARED something AS STRING ' SHARED only, not DIM
  PRINT something ' Prints "anything, really"
END SUB

SUB test2
  PRINT something ' Prints "0", because <something> is undefined in this routine and QB assumes it is an integer
END SUB
Just posting this, in case anyone stumbles upon this thread.
Erik
Veteran
Posts: 72
Joined: Wed Jun 20, 2007 12:31 pm
Location: LI, NY
Contact:

Re: how to share Common declare between modules

Post by Erik »

MikeHawk wrote: Fri Jan 10, 2020 3:56 pm It works with interpreted source code in the IDE, or with BRUN45-dependent EXEs.
Really good info posted above.

QQ on the quoted text. Does this mean that a "stand alone" compiled EXE with COMMON vars will not carry over if the program isn't recompiled to require BRUN45?
MikeHawk
Veteran
Posts: 61
Joined: Sun Jul 08, 2018 11:23 am

Re: how to share Common declare between modules

Post by MikeHawk »

Erik wrote: Sat May 02, 2020 3:36 am Does this mean that a "stand alone" compiled EXE with COMMON vars will not carry over if the program isn't recompiled to require BRUN45?
Yep.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: how to share Common declare between modules

Post by burger2227 »

COMMON SHARED only passes variables to modules called with CHAIN

Modules are compiled and SUB's and FUNCTION's are passed to each EXE through CHAIN.
CHAIN can work with ALL BAS or EXE modules, not a combination. one or the other.

One possible reason to use multiple modules is when OUT OF STRING SPACE errors start...

The COMMON SHARED values are passed in order listed and the variable names can be
changed in each module as they are listed in order from left to right with proper types.
The shared variable lists MUST MATCH the list of comma separated variable types of the original call!
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
Erik
Veteran
Posts: 72
Joined: Wed Jun 20, 2007 12:31 pm
Location: LI, NY
Contact:

Re: how to share Common declare between modules

Post by Erik »

Awesome. Thanks for the info!!
Post Reply