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

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
Coder
Posts: 18
Joined: Sun Jul 08, 2018 11:23 am

Re: how to share Common declare between modules

Post by MikeHawk » Fri Jan 10, 2020 3:56 pm

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

Post Reply