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.