Author Topic: Sharing variables - problem  (Read 2115 times)

0 Members and 1 Guest are viewing this topic.

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Sharing variables - problem
« on: March 17, 2020, 06:09:40 am »
Trying out sharing variables - what is wrong/missing with code below (c% not passed). If enable COMMON SHARED c% the program works correctly.


'COMMON SHARED c%

TYPE Cs
    c AS INTEGER
END TYPE
DIM Cs AS Cs

c% = 2
PRINT "Main Module  C% = ";: PRINT c%
MySUB
PRINT "Main Module  C% = ";: PRINT c%
END

SUB MySUB ()
    SHARED Cs AS Cs
    PRINT "SUB TEST ()  C% = ";: PRINT c%
    c% = -1
    PRINT "SUB TEST ()  C% = ";: PRINT c%
END SUB

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Sharing variables - problem
« Reply #1 on: March 17, 2020, 06:52:51 am »
I suspect you may be a little confused, because the Cs variable is not being used at all in the program. For instance, here's an equivalent program to what you posted, with unused parts removed:
Code: QB64: [Select]
  1. 'COMMON SHARED c%
  2.  
  3. c% = 2
  4. PRINT "Main Module  C% = ";: PRINT c%
  5. MySUB
  6. PRINT "Main Module  C% = ";: PRINT c%
  7.  
  8. SUB MySUB ()
  9.     PRINT "SUB TEST ()  C% = ";: PRINT c%
  10.     c% = -1
  11.     PRINT "SUB TEST ()  C% = ";: PRINT c%
  12.  

The c% in the main program is entirely independent from the c% in MySUB, unless you uncomment the COMMON SHARED c% declaration at the top. Then references to c% in MySUB become references to c% in the main program.

On a side note, no need for "COMMON". "DIM SHARED c%" is considered (by me, anyway - don't take me too seriously) neater.

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: Sharing variables - problem
« Reply #2 on: March 17, 2020, 07:42:19 am »
Thanks Luke for your reply.

Not that it really matters (as you have provided a "corrected" solution) …


Just for my interest, can you adjust my code to use the Type variable Cs as being the shared variable?


Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Sharing variables - problem
« Reply #3 on: March 17, 2020, 08:12:55 am »
Sure.

Code: QB64: [Select]
  1. TYPE Ctype
  2.     c AS INTEGER
  3.  
  4. DIM SHARED Cs AS Ctype
  5.  
  6. Cs.c = 2
  7. PRINT "Main Module: "; Cs.c
  8. MySUB
  9. PRINT "Main Module: "; Cs.c
  10.  
  11. SUB MySUB ()
  12.     PRINT "SUB TEST (): "; Cs.c
  13.     Cs.c = -1
  14.     PRINT "SUB TEST (): "; Cs.c

Now, I've fiddled a few other things because I'm strongly opinionated; others' tastes may be different:
 - I renamed the User-Defined Type so it's different to the variable itself. I find it confusing when the same name is used in two different places.
 - There is no "SHARED" line inside the sub, instead the Cs variable is DIM SHARED in the main program. I never use the SHARED-inside-a-sub technique because I get lost keeping track of everything.
 - You can print multiple things by just separating them with ";", no need for another PRINT keyword :)

The main magic here is that you create a variable (Cs) with DIM SHARED, then that variable and all its fields can be used by all subs (and functions, of course).

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: Sharing variables - problem
« Reply #4 on: March 17, 2020, 08:55:15 am »
Thankyou very much Luke.

Works exactly how I wanted it too (the print statements only used for diagnostic).

Now I understand much better how to apply the TYPE functionality, though  in my simple example there is no benefit over your first answer.