Author Topic: A better way to handle COMMON SHARED / SHARED ?  (Read 3829 times)

0 Members and 1 Guest are viewing this topic.

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
A better way to handle COMMON SHARED / SHARED ?
« 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?

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: A better way to handle COMMON SHARED / SHARED ?
« Reply #1 on: March 04, 2020, 03:32:26 am »
I would question the need for 100 globals to begin with - the majority of data should be via function arguments.

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: A better way to handle COMMON SHARED / SHARED ?
« Reply #2 on: March 04, 2020, 03:33:14 am »
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
« Last Edit: March 04, 2020, 03:36:39 am by TerryRitchie »
In order to understand recursion, one must first understand recursion.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: A better way to handle COMMON SHARED / SHARED ?
« Reply #3 on: March 04, 2020, 10:00:55 am »
Hi Richard

sorry I'm parsing your request

COMMON SHARED 
Quote
COMMON shares common variable values with other linked or CHAINed modules.

DIM SHARED   
Quote
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
Code: QB64: [Select]
  1. i = 10
  2. a
  3. b
  4.  
  5.  
  6. SUB a
  7.     PRINT i
  8.     i = i + 1
  9.  
  10. SUB b
  11.     PRINT i
  12.     i = i + 1
  13.     c
  14.  
  15. SUB c
  16.     PRINT i
  17.     i = i + 1
  18.  
  19.  

I hope that this will be useful!
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A better way to handle COMMON SHARED / SHARED ?
« Reply #4 on: March 04, 2020, 12:20:25 pm »
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.

Code: QB64: [Select]
  1. DIM SHARED mySharedVar
  2. mySharedVar = 10
  3. testShared
  4. SUB testShared
  5.     SHARED mySharedVar ' <<<<<<<<<<<<<<<<<<< this line is not necessary
  6.     PRINT "My Shared Variable:"; mySharedVar
  7.  

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!
« Last Edit: March 04, 2020, 12:31:28 pm by bplus »

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: A better way to handle COMMON SHARED / SHARED ?
« Reply #5 on: March 04, 2020, 12:29:45 pm »
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.
Granted after becoming radioactive I only have a half-life!

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: A better way to handle COMMON SHARED / SHARED ?
« Reply #6 on: March 04, 2020, 12:41:36 pm »
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
« Last Edit: March 04, 2020, 05:29:27 pm by Richard »