SHARED doesn't share variables from the non-main-scope

Just starting out? Need help? Post your questions and find answers here.
rudd68
User
User
Posts: 29
Joined: Wed Jun 09, 2010 2:55 pm

SHARED doesn't share variables from the non-main-scope

Post 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.
Last edited by rudd68 on Wed Jul 21, 2010 9:28 pm, edited 1 time in total.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: SHARED doesn't work in sub-procedures

Post 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!
rudd68
User
User
Posts: 29
Joined: Wed Jun 09, 2010 2:55 pm

Re: SHARED doesn't work in sub-procedures

Post 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.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: SHARED doesn't work in sub-procedures

Post 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 :wink:
Last edited by netmaestro on Wed Jul 21, 2010 4:59 am, edited 2 times in total.
BERESHEIT
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: SHARED doesn't work in sub-procedures

Post 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
rudd68
User
User
Posts: 29
Joined: Wed Jun 09, 2010 2:55 pm

Re: SHARED doesn't work in sub-procedures

Post 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.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: SHARED doesn't work in sub-procedures

Post 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.
BERESHEIT
rudd68
User
User
Posts: 29
Joined: Wed Jun 09, 2010 2:55 pm

Re: SHARED doesn't work in sub-procedures

Post 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.
Fred
Administrator
Administrator
Posts: 18161
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: SHARED doesn't work in sub-procedures

Post by Fred »

It's the intended behaviour, shared is only applicable to one procedure.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: SHARED doesn't work in sub-procedures

Post 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 ;)
quidquid Latine dictum sit altum videtur
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: SHARED doesn't work in sub-procedures

Post 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
rudd68
User
User
Posts: 29
Joined: Wed Jun 09, 2010 2:55 pm

Re: SHARED doesn't work in sub-procedures

Post 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.
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: SHARED doesn't work in sub-procedures

Post 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
rudd68
User
User
Posts: 29
Joined: Wed Jun 09, 2010 2:55 pm

Re: SHARED doesn't work in sub-procedures

Post by rudd68 »

OK, how can I change the topic title?
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: SHARED doesn't work in sub-procedures

Post by Vera »

Hi,

just go to your first posting and click 'edit' and you can change the title as well
Post Reply