Page 1 of 2
SHARED doesn't share variables from the non-main-scope
Posted: Tue Jul 20, 2010 8:14 pm
by rudd68
Hello,
the keyword "Shared" doesn't work, but it isn't forbidden in the documentation:
Code: Select all
Procedure.s Create_Display_Text()
Shared Element
Debug Element
Text.s = "Random_Element is: " + Str(Element)
ProcedureReturn Text
EndProcedure
Procedure Random_Element()
Element.l = Random(100) + 1
Debug Element
Debug Create_Display_Text()
EndProcedure
; main program
Random_Element()
End
Please don't correct the documentation but enlarge the functionality of the keyword "Shared".
Greetings.
Re: SHARED doesn't work in sub-procedures
Posted: Tue Jul 20, 2010 8:25 pm
by ts-soft
You have to use shared in all procedures, you use it.
Code: Select all
Procedure.s Create_Display_Text()
Shared Element
Debug Element
Text.s = "Random_Element is: " + Str(Element)
ProcedureReturn Text
EndProcedure
Procedure Random_Element()
Shared Element
Element = Random(100) + 1
Debug Element
Debug Create_Display_Text()
EndProcedure
; main program
Random_Element()
End
And it is recommed to define the Shared Var in the MainScope!
Re: SHARED doesn't work in sub-procedures
Posted: Tue Jul 20, 2010 8:52 pm
by rudd68
Thank you ts-soft.
But the first definition of the variable "Element" is in the procedure "Random_Element()".
The behavior of "Shared" is differently to written in the documentation.
I have 1100 lines of code, 15 procedures, many variables. Your solution expands the main program code and the code in the higher procedures. I don't want this.
Either I use parameters in the procedure calls or I use the keyword "Global". "Shared" is too restricted.
Re: SHARED doesn't work in sub-procedures
Posted: Tue Jul 20, 2010 9:01 pm
by netmaestro
You have to use shared in all procedures, you use it.
You only need to use the keyword Shared in those procedures that need access to the var. Or, alternatively, define the var in the main scope and use
Shared in one or more procedures that need access. If you have 15 procedures, by no means do all procedures need the definition. That would mimic Global. One common use for it is if you have a procedure and you're calling something like EnumChildWindows() or such from it. You would use Shared to allow those two procs to know a particular variable, where none of the others know it or need to:
Code: Select all
Procedure four( )
Debug "four: "+Str(x) ; not shared here!
EndProcedure
Procedure three( )
Debug "three: "+Str(x) ; or here!
four()
EndProcedure
Procedure two()
Shared x
Debug "two: "+Str(x)
three()
EndProcedure
Procedure one()
Shared x
x=10
Debug "one: "+Str(x)
two()
EndProcedure
one()
As I hope is obvious by now, a
Shared variable must exist in a minimum of two places in your code to make any sense (and actually do something useful). If it exists in only one place, there is nobody to share with. This is what was wrong with the posted snippet.
The EnumChildWindows is a good example because the EnumProc can't return anything. You want to know something from it, you share a var in the caller and the EnumProc, then let EnumProc set it. The caller receives it because of Shared.
I hope this helps to clear it up for you, as Shared isn't bugged and is quite useful just as it is.
@ts-soft: I know you understand all this, I think your english let you down a bit on this one

Re: SHARED doesn't work in sub-procedures
Posted: Tue Jul 20, 2010 9:55 pm
by Vera
@netmaestro
your description and example are very impressionably and would be a fine addition to the IDE-help
THANKS
also for asking
@ rudd68
what would you think about changing the thread-title into something like: 'How SHARED works in sub-procedures'
greetings ~ Vera
Re: SHARED doesn't work in sub-procedures
Posted: Tue Jul 20, 2010 10:02 pm
by rudd68
Thank you netmaestro.
Shared must exist in a minimum of two places in your code to make any sense (and actually do something useful). If it exists in only one place, there is nobody to share with.
I doesn't agree with you. Please see the first example in the "Shared" documentation. There is only one "Shared" keyword. It shares the variable "a" from the main program to the procedure.
The solution by ts-soft and your example shares the variable from the main program down to all procedures and sub-procedures which have the "Shared" keyword. If the variable is not defined in the main program then it is automatically (implicit) defined at the first "Shared" keyword. The evidence: Use "EnableExplicit" and the first "Shared" keyword (in the logical highest procedure) gives an error ("Variables must be defined").
I doesn't want to share a variable from the main program. I want to share a variable, which is first defined in a procedure. This isn't excluded by the documentation.
@vera:
netmaestro's explanation is only partly correct.
I doesn't want rename the title, because "Shared" doesn't works in sub-procedures, if the variable is first defined in the caller procedure.
Re: SHARED doesn't work in sub-procedures
Posted: Tue Jul 20, 2010 10:04 pm
by netmaestro
I doesn't agree with you. Please see the first example in the "Shared" documentation.
The keyword
Shared doesn't belong in the main scope, but the variable must exist there if you want to share it between main scope and a proc. Trust me, there's no bug here and the doc is right.
Please bring stuff like this up in Coding Questions.
Re: SHARED doesn't work in sub-procedures
Posted: Tue Jul 20, 2010 10:25 pm
by rudd68
I think it is a bug or an inaccurate documentation. If it is not a bug then it is my feature wish (and the documentation should be updated.)
Greetings.
Re: SHARED doesn't work in sub-procedures
Posted: Tue Jul 20, 2010 10:33 pm
by Fred
It's the intended behaviour, shared is only applicable to one procedure.
Re: SHARED doesn't work in sub-procedures
Posted: Wed Jul 21, 2010 12:02 am
by freak
> I want to share a variable, which is first defined in a procedure. This isn't excluded by the documentation.
It is not explicitly excluded, but the documentation also doesn't say you can do that. If we try to document everything you _can't_ do with PB, then the help will get very large

Re: SHARED doesn't work in sub-procedures
Posted: Wed Jul 21, 2010 12:19 pm
by Little John
Just a small comment, because the title of this thread has the potential for confusing newbies:
Thread title wrote:SHARED doesn't work in sub-procedures
In PB, there are procedures, but no sub-procedures.
A sub-procedure would be a procedure inside of another procedure.
Regards, Little John
Re: SHARED doesn't work in sub-procedures
Posted: Wed Jul 21, 2010 7:04 pm
by rudd68
@fred, freak: Thank you for your posts. So I know that the topic has reached you.
@Little John: If procedure "One()" calls procedure "Two()", then procedure "Two()" isn't a sub-procedure? How would you describe procedure "Two()" (I'm from germany and I doesn't have the correct english word)?
A sub-procedure would be a procedure inside of another procedure.
There are sub-procedures in other programming languages like this ???:
Code: Select all
Procedure one()
; code of procedure one()
Procedure two()
; code of procedure two()
EndProcedure ; ends procedure definition two()
; code of procedure one()
EndProcedure ; ends procedure definition one()
Is there a meaning for it?
Greetings.
Re: SHARED doesn't work in sub-procedures
Posted: Wed Jul 21, 2010 7:49 pm
by Little John
rudd68 wrote:@Little John: If procedure "One()" calls procedure "Two()", then procedure "Two()" isn't a sub-procedure?
According to my understanding: no.
rudd68 wrote:How would you describe procedure "Two()"
I'd call (no pun intended) procedure "One()" the caller, and procedure "Two()" the callee.
rudd68 wrote:(I'm from germany and I doesn't have the correct english word)?
I'm not a native English speaker, either. However, IMHO that term carries the risk of creating confusion. And I've never read the term "sub-procedure" in connection with PureBasic. In the docs and everywhere it always just reads "procedure".
rudd68 wrote:A sub-procedure would be a procedure inside of another procedure.
There are sub-procedures in other programming languages like this ???:
Code: Select all
Procedure one()
; code of procedure one()
Procedure two()
; code of procedure two()
EndProcedure ; ends procedure definition two()
; code of procedure one()
EndProcedure ; ends procedure definition one()
Yes, there are; for instance in the programming language Pascal. I personally never had any need for that, though.
Regards, Little John
Re: SHARED doesn't work in sub-procedures
Posted: Wed Jul 21, 2010 8:58 pm
by rudd68
OK, how can I change the topic title?
Re: SHARED doesn't work in sub-procedures
Posted: Wed Jul 21, 2010 9:02 pm
by Vera
Hi,
just go to your first posting and click 'edit' and you can change the title as well