![]() |
|
Permitted entries for Common Shared - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: Permitted entries for Common Shared (/showthread.php?tid=2199) |
Permitted entries for Common Shared - PhilOfPerth - 11-24-2023 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.
RE: Permitted entries for Common Shared - TerryRitchie - 11-24-2023 (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. 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. RE: Permitted entries for Common Shared - PhilOfPerth - 11-24-2023 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? RE: Permitted entries for Common Shared - mnrvovrfc - 11-24-2023 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. RE: Permitted entries for Common Shared - TerryRitchie - 11-24-2023 (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. RE: Permitted entries for Common Shared - SpriggsySpriggs - 11-24-2023 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. RE: Permitted entries for Common Shared - TerryRitchie - 11-24-2023 (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) ' _____________________________________________________________________________________________________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. RE: Permitted entries for Common Shared - PhilOfPerth - 11-24-2023 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. RE: Permitted entries for Common Shared - TerryRitchie - 11-24-2023 (11-24-2023, 11:02 PM)PhilOfPerth Wrote: OK. got it. Thanks for the clarification.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. RE: Permitted entries for Common Shared - PhilOfPerth - 11-24-2023 Thanks Terry. I re-read chapter 6 and this clarified things further for me. Great job! |