Posts: 652
Threads: 96
Joined: Apr 2022
Reputation:
22
11-24-2023, 01:54 AM
(This post was last modified: 11-24-2023, 01:55 AM by PhilOfPerth.)
I've noticed that when using Common Shared in my progs, it picks up if I declare a variable twice, and disallows it.
But it lets me declare arrays more than once - eg Common Shared Names$(), Names$().
Is this just an oversight? Or if it's intended, how can it be used?
Not Earth-shattering, I'm just curious.
Posts: 1,272
Threads: 119
Joined: Apr 2022
Reputation:
100
(11-24-2023, 01:54 AM)PhilOfPerth Wrote: I've noticed that when using Common Shared in my progs, it picks up if I declare a variable twice, and disallows it.
But it lets me declare arrays more than once - eg Common Shared Names$(), Names$().
Is this just an oversight? Or if it's intended, how can it be used?
Not Earth-shattering, I'm just curious.
That's more than likely an oversight. Why are you using COMMON SHARED? That's used to link multiple programs and share variables between them, unless of course that is what you're doing.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Posts: 652
Threads: 96
Joined: Apr 2022
Reputation:
22
I'm using it to share variables between the Main and Subroutines, as I often have done - I thought that was the main purpose of this. Wrong again?
Posts: 1,586
Threads: 59
Joined: Jul 2022
Reputation:
52
DIM is enough instead of COMMON.
Remember that QB64 cannot pass the values of variables from one program to another, both created by the compiler. The COMMON SHARED trick worked best in QuickBASIC, for programs compiled for the runtime module BRUN45.EXE.
Somehow I wish QB64 could do that sort of thing but it's not realistic enough. It could present a problem with security.
Posts: 1,272
Threads: 119
Joined: Apr 2022
Reputation:
100
11-24-2023, 03:02 PM
(This post was last modified: 11-24-2023, 03:02 PM by TerryRitchie.)
(11-24-2023, 08:34 AM)PhilOfPerth Wrote: I'm using it to share variables between the Main and Subroutines, as I often have done - I thought that was the main purpose of this. Wrong again? Use DIM SHARED instead, that will make all your variables global by being shared everywhere.
If you just want to share a variable with a subroutine/function then you can do this:
DIM Test%
SUB MySub()
SHARED Test%
... your code here
END SUB
Test% will be local to the main code and SHARED with the MySub() subroutine.
I have a complete explanation of this in Lesson 6 here in the tutorial.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Posts: 733
Threads: 30
Joined: Apr 2022
Reputation:
43
Personally, I am very much against shared variables and use them absolutely sparingly. However, Terry's suggestion is far better than just a global variable.
Tread on those who tread on you
Posts: 1,272
Threads: 119
Joined: Apr 2022
Reputation:
100
11-24-2023, 03:58 PM
(This post was last modified: 11-24-2023, 04:00 PM by TerryRitchie.)
(11-24-2023, 03:15 PM)SpriggsySpriggs Wrote: Personally, I am very much against shared variables and use them absolutely sparingly. However, Terry's suggestion is far better than just a global variable.
Same here. The nice thing about using SHARED within subroutines and functions is that is creates a nice little table of variables used by the sub/function. Take this little snippet of code for example:
Code: (Select All) ' _____________________________________________________________________________________________________
'/ \
SUB SortGroceryList () ' SortGroceryList |
' _________________________________________________________________________________________________|____
'/ \
'| Sorts the grocery list alphabetically. |
'\______________________________________________________________________________________________________/
SHARED Groceries() AS GROCERIES ' Array containing user generated grocery list
SHARED GroceriesList() AS STRING ' Scrollselect list that points to Groceries()
DIM Inner AS INTEGER ' inner sort loop counter
DIM Outer AS INTEGER ' outer sort loop counter
'+-------------------------+
'| Bubblesort grocery list |
'+-------------------------+
FOR Outer = 1 TO UBOUND(Groceries) - 1 ' loop array size-1
FOR Inner = 1 TO UBOUND(groceries) - Outer ' loop remaining indexes
IF UCASE$(Groceries(Inner).Name) > UCASE$(Groceries(Inner + 1).Name) THEN ' next index smaller?
SWAP Groceries(Inner), Groceries(Inner + 1) ' yes, swap indexes (bubble up)
SWAP GroceriesList(Inner), GroceriesList(Inner + 1)
END IF
NEXT Inner
NEXT Outer
END SUB
The two SHARED statements tell me exactly which variables outside of the subroutine will be used and affected. I can now use the find feature in the IDE to search for either "SHARED Groceries() AS GROCERIES" or "SHARED GroceriesList() as STRING" to find all subroutines and functions that SHARE these same variables and can be affected by this subroutine's actions.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Posts: 652
Threads: 96
Joined: Apr 2022
Reputation:
22
11-24-2023, 11:02 PM
(This post was last modified: 11-24-2023, 11:02 PM by PhilOfPerth.)
OK. got it. Thanks for the clarification.
A problem not shared is a problem solved, to paraphrase an old saying.
I'll use the Dim Shared, and limit it to subs unless needed everywhere, as Terry suggested.
Posts: 1,272
Threads: 119
Joined: Apr 2022
Reputation:
100
(11-24-2023, 11:02 PM)PhilOfPerth Wrote: OK. got it. Thanks for the clarification.
A problem not shared is a problem solved, to paraphrase an old saying.
I'll use the Dim Shared, and limit it to subs unless needed everywhere, as Terry suggested. I think you'll find your code easier to manage if you do this. There's nothing wrong however with making all your variables global with DIM SHARED if that is easier for you.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Posts: 652
Threads: 96
Joined: Apr 2022
Reputation:
22
11-24-2023, 11:24 PM
(This post was last modified: 11-24-2023, 11:41 PM by PhilOfPerth.)
Thanks Terry. I re-read chapter 6 and this clarified things further for me. Great job!
|