Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Permitted entries for Common Shared
#7
(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
Reply


Messages In This Thread
RE: Permitted entries for Common Shared - by TerryRitchie - 11-24-2023, 03:58 PM



Users browsing this thread: 1 Guest(s)