QB64.org Forum
Active Forums => QB64 Discussion => Topic started by: Richard on March 04, 2020, 03:23:43 am
-
If I have say 100 variables in a program which does not use SUBs/FUNCTIONs - only uses GOSUBs - then I do not need to COMMON SHARED / SHARED.
However, if I have say 10 SUBs each with 10 variables used, and many of those SUBs can call any of the other SUBS with some amount of same variable usage between those SUBS - I currently COMMON SHARED (my 100 variables) and then for each of the 10 SUBs I SHARED (my 100 variables), knowing only a few are referenced for each SUB.
This SHARED approach "clutters' an otherwise compact SUB and if I make a typo in the SUBs SHARED variables - can cause time wasting solving problems.
Apart from $include approach for the SHARED variables in the SUBs - is there a better way to approach this?
-
I would question the need for 100 globals to begin with - the majority of data should be via function arguments.
-
There are a few ways I approach this:
1st - Using SHARED
DIM Something(10) AS INTEGER
SUB MySub()
SHARED Something() AS INTEGER
END SUB
2nd - Using shared but with a type to move lots of information
TYPE LOTSOFDATA
Var1 AS INTEGER
Var2 AS LONG
Var3 AS STRING
etc..
etc..
END TYPE
DIM LotsOfData AS LOTSOFDATA
SUB MySub()
SHARED LotsOfData AS LOTSOFDATA
END SUB
3rd - Pass the TYPE in
MySub LotsOfData
SUB MySub(Stuff AS LOTSOFDATA)
PRINT Stuff.Var1
PRINT Stuff.Var2
etc..
etc..
END SUB
-
Hi Richard
sorry I'm parsing your request
COMMON SHARED COMMON shares common variable values with other linked or CHAINed modules.
DIM SHARED DIMensioned variables are shared with all procedures in the program module.
When used with DIM in the main module, it eliminates the need to pass a parameter variable to a SUB or FUNCTION.
IMHO using DIM SHARED you needn't use SHARED into Subs/Functions or pass them as parameters.
see this example
i = 10
a
b
i = i + 1
i = i + 1
c
i = i + 1
I hope that this will be useful!
-
If I have say 100 variables in a program which does not use SUBs/FUNCTIONs - only uses GOSUBs - then I do not need to COMMON SHARED / SHARED.
However, if I have say 10 SUBs each with 10 variables used, and many of those SUBs can call any of the other SUBS with some amount of same variable usage between those SUBS - I currently COMMON SHARED (my 100 variables) and then for each of the 10 SUBs I SHARED (my 100 variables), knowing only a few are referenced for each SUB.
This SHARED approach "clutters' an otherwise compact SUB and if I make a typo in the SUBs SHARED variables - can cause time wasting solving problems.
Apart from $include approach for the SHARED variables in the SUBs - is there a better way to approach this?
I am not understanding the problem.
Do you know that if you DIM SHARED a variable or array at the beginning of a program then you do NOT have to use SHARED in all the subs using those variables.
mySharedVar = 10
testShared
SHARED mySharedVar
' <<<<<<<<<<<<<<<<<<< this line is not necessary PRINT "My Shared Variable:"; mySharedVar
PS save yourself from lost time from typos by OPTION _EXPLICIT first line of your program, you will never know all the grieve you just saved yourself from but it's not free you pay by DIM everything!
-
I'm with Bplus on this. Don't really understand what your getting at.
Perhaps a little code as an example?
I mean no offense, I'm terrible at explaining things most the time myself.
-
Thanks everyone for your replies.
I was doing things the way I did with M$ PDS 7.1 (an upgrade to QB45) and at that time I did not have access to internet and forums and support groups or "experts".
Suppose you could say "old habits die hard".
Many thanks