Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shared common variables

Status
Not open for further replies.

Andrew Gable

Programmer
Jan 7, 2020
3
0
0
GB
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

Public sub PrintTerminalNumber
Print TerminalNumber
End sub

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

I even have tried to declare it like

public sub PrintTermialNumber (byval termID string)

And the. Run call PrintTerminalNumber (TerminalNumber) but it print a blank entry

I would appreciate any help or example
Code on this


Thanks

Andy
 
What compiler are you using? Original old DOS QBasic or QB64?
 
I tried it only in QB64.
Passing data to subroutines as parameters or via shared variables works like in this example:

mainpgm.bas
Code:
DIM a AS STRING
DIM SHARED b AS STRING
COMMON SHARED c AS STRING

CLS

a = "foo"
b = "bar"
c = "baz"
PRINT "Main program:"
PRINT "a = " + a
PRINT "b = " + b
PRINT "c = " + c

CALL sub1(a$)

CALL sub2(a$)

END
'------------ subroutine -----------
SUB sub1 (x$)
    PRINT "* calling SUB1:"
    'a is not normally visible in SUB1, it must be SHARED
    SHARED a$
    PRINT "  a = " + a$
    PRINT "  b = " + b$
    PRINT "  c = " + c$
    PRINT "  x = " + x$
    PRINT "**"
END SUB

'--------- included module ---------
'$INCLUDE: 'module01.BM'

module01.bm
Code:
SUB sub2 (x$)
    PRINT "* calling SUB2 from the module:"
    'a is not normally visible in SUB2, it must be SHARED
    SHARED a$
    PRINT "  a = " + a$
    PRINT "  b = " + b$
    PRINT "  c = " + c$
    PRINT "  x = " + x$
    PRINT "**"
END SUB

Output of mainpgm.exe:
mainpgm_b39p5g.jpg
 
I think I just spotted what I need to do

SHARED a$

would i include that like this


SHARED a$


Code:
Public sub PrintTerminalNumber
SHARED TerminalNumber 
Print TerminalNumber
End sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top