Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Permitted entries for Common Shared
#1
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.  Undecided
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#2
(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.  Undecided

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
Reply
#3
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?
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#4
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.
Reply
#5
(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
Reply
#6
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

Reply
#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
#8
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.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#9
(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
Reply
#10
Thanks Terry. I re-read chapter 6 and this clarified things further for me. Great job!
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply




Users browsing this thread: 3 Guest(s)