Page 1 of 1

how to share Common declare between modules

Posted: Tue Jan 07, 2020 10:16 am
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

Re: how to share Common declare between modules

Posted: Fri Jan 10, 2020 3:56 pm
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

Re: how to share Common declare between modules

Posted: Mon Apr 13, 2020 5:43 am
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.

Re: how to share Common declare between modules

Posted: Sat May 02, 2020 3:36 am
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?

Re: how to share Common declare between modules

Posted: Sat May 02, 2020 10:05 am
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.

Re: how to share Common declare between modules

Posted: Sat May 02, 2020 1:05 pm
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!

Re: how to share Common declare between modules

Posted: Sat Jun 20, 2020 3:03 am
by Erik
Awesome. Thanks for the info!!