Author Topic: Dim Shared v's Parameters & Arguments  (Read 6231 times)

0 Members and 1 Guest are viewing this topic.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Dim Shared v's Parameters & Arguments
« Reply #15 on: December 15, 2020, 04:21:32 pm »
Hi Steve

Ya, it's your rule #1 which  is what I was referring to in the comment above about Dim Shared Arrays as being dicey.

Thanks Steve



Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Dim Shared v's Parameters & Arguments
« Reply #16 on: December 15, 2020, 06:40:52 pm »
Hi
nothing to add to your professional experience, but I must remark this:  [ You are not allowed to view this attachment ]  
Programming isn't difficult, only it's  consuming time and coffee

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Dim Shared v's Parameters & Arguments
« Reply #17 on: December 16, 2020, 08:19:27 am »
Thanks Tempodi - you are right - I did not consider Shared.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Dim Shared v's Parameters & Arguments
« Reply #18 on: December 16, 2020, 12:14:24 pm »
Thanks Tempodi - you are right - I did not consider Shared.

I thought he was talking about CONST's that are automatically SHARED, we've been talking about SHARED all along.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Dim Shared v's Parameters & Arguments
« Reply #19 on: December 16, 2020, 01:03:04 pm »
I thought he was talking about CONST's that are automatically SHARED, we've been talking about SHARED all along.


He's talking about:

SUB example
    SHARED foo
    ... do junk
END SUB

It's a way to share information between subs and the main module, without having to rely on passing via parameters.  For me, I generally tend to go this way when there's unchanging variables like the ScreenX, ScreenY resolutions which I talked about above, or when my parameter list has gotten so long it's unwieldly to keep as a single line, such as below:

SUB Boxed_Text (x, y, wide, tall, text$)
    Shared font, fontcolor, fontbackground, workscreen, framecolor, framethickness, fontplacement
    Shared fontstyle, captionstyle, captionthick, captionfont, captionfontcolor, captioncolor
    Shared close_button, minimize_button, maximize_button, move_button, scrollbar_vert, scrollbar_hort
    ... do stuff
END SUB

Many of these things might not change from call to call to the SUB, and it'd be impossible to keep up with all those dang options and ever use the command properly, so the main changeable stuff is passed via parameter, and the rest is passed via SHARED inside the sub itself.


https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Dim Shared v's Parameters & Arguments
« Reply #20 on: December 16, 2020, 04:29:08 pm »
Don't forget the use in TYPE...

Code: QB64: [Select]
  1. TYPE democrat
  2.     dumb AS STRING
  3.  
  4. DIM wit AS democrat
  5.  

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Dim Shared v's Parameters & Arguments
« Reply #21 on: December 16, 2020, 04:56:47 pm »
@Pete  thanks for sharing.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Dim Shared v's Parameters & Arguments
« Reply #22 on: December 16, 2020, 08:36:10 pm »
"sharing" :D :D :D
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Dim Shared v's Parameters & Arguments
« Reply #23 on: December 17, 2020, 03:52:19 pm »
Please tell me
Sharing what? Wit, Dumb or Democrat?
Programming isn't difficult, only it's  consuming time and coffee

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Dim Shared v's Parameters & Arguments
« Reply #24 on: December 17, 2020, 06:04:38 pm »
None of the above. Making it functional, using SHARED, just spoils one of the the puns. Okay, fine, but now it requires more explanation...

Code: QB64: [Select]
  1. TYPE democrat
  2.     dumb AS STRING
  3.  
  4. DIM SHARED dimwit AS democrat
  5.  
  6. dimwit.dumb = "All Democrats are idiots.": CALL idiots
  7.  
  8. SUB idiots
  9. PRINT dimwit.dumb
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Dim Shared v's Parameters & Arguments
« Reply #25 on: December 17, 2020, 06:59:10 pm »
Now THAT should have been in the voting machines. ;)

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Dim Shared v's Parameters & Arguments
« Reply #26 on: December 17, 2020, 09:18:16 pm »
Well Andy, I'm pretty sure Dominion software is coded in FreeBASIC, so what could go wronnnnnnng?

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Dim Shared v's Parameters & Arguments
« Reply #27 on: December 17, 2020, 09:33:13 pm »
And here's one final tip: case your shared variables differently to you local ones.

So if you normally write local variables as 'local_var', consider starting shared variables with a capital letter: 'Shared_var'.

That way when you're reading a section of code you know immediately what's coming from outside the subroutine, and whether changing a variable's value will affect other parts of the code.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Dim Shared v's Parameters & Arguments
« Reply #28 on: December 18, 2020, 08:44:44 am »
Hey Luke - that's a helpful tip. Thanks for same.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Dim Shared v's Parameters & Arguments
« Reply #29 on: December 18, 2020, 10:27:03 am »
And here's one final tip: case your shared variables differently to you local ones.

So if you normally write local variables as 'local_var', consider starting shared variables with a capital letter: 'Shared_var'.

That way when you're reading a section of code you know immediately what's coming from outside the subroutine, and whether changing a variable's value will affect other parts of the code.

Only old people of diminished mental capacity would ever need to use a "trick" like that to sort out shared variables from local variables... Thanks for the tip, Fred.

Jack
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/