Page 1 of 1
Global vs. Shared variables
Posted: Sat Jul 05, 2003 8:42 pm
by Tipperton
What exactly is the difference between a global variable and a shared variable?
The way I read it from the help file they appear to be the same thing.
Posted: Sat Jul 05, 2003 8:54 pm
by plouf
when you create a Global variable it is accesible everywhere in your program
a shared declared inside a procedure and it is a variable of your main program accessible inside this procedure only
i have problem to understand it clearly in beggining too

until someone explain it to me
Posted: Sat Jul 05, 2003 10:08 pm
by ebs
I agree with you; in PB there isn't much difference at all. Shared variables are like static variables in C - they retain their value between procedure calls. However, they
are accessible from the main program, which is not the case in C.
A simple example:
Code: Select all
Procedure.l AddOneShared()
Shared SValue.l
SValue + 1
ProcedureReturn SValue
EndProcedure
Debug "Shared = " + Str(SValue)
Debug ""
For n = 1 To 10
Debug "Shared = " + Str(AddOneShared())
Next
Debug ""
Debug "Shared = " + Str(SValue)
SValue = 99
Debug ""
Debug "Shared = " + Str(SValue)
Debug ""
Debug "Shared = " + Str(AddOneShared())
Debug ""
Debug "Shared = " + Str(SValue)
Notice that the value of SValue can be read and set from the main program.
Regards,
Eric
Shared have to be in main?
Posted: Sat Sep 13, 2003 8:58 am
by Andy
Hello again
I have here still a question to shared variables: It seems to be, that shared variables >have< to be known in the main program:
Code: Select all
a.l = 10
;b.l=30
;run the program one time without the definition "b.l=30"
;and another time with it and you can see...
Procedure p2()
Shared a, b
Debug "p2 ------"
Debug a
Debug b
EndProcedure
Procedure p1()
Shared a
;Another possibility to make this code running "correct":
;Shared a,b
b.l=20
Debug "p1 ------"
Debug a
Debug b
p2()
EndProcedure
p1()
End
Am I right - the shared have to be in the main, it's not possible to make a shared, which is only known in the procedures?
Best Regards
Andy
Posted: Sat Sep 13, 2003 2:39 pm
by ebs
Andy,
It seems to be, that shared variables >have< to be known in the main program:
I don't think that's exactly correct. In your example, variable a is shared in both procedures p1 and p2. Therefore, when you set it's value in the main program, it can be "seen" in both procedures. However, variable b is shared
only in procedure p2. When you set the variable b in procedure p1, you are setting the value of a
local variable, visible only in that procedure.
If you want to demonstrate that a variable can be shared in two procedures but
not in the main program, try this version of your example:
Code: Select all
;a.l = 10 <-- commented out
;b.l=30
;run the program one time without the definition "b.l=30"
;and another time with it and you can see...
Procedure p2()
Shared a, b
Debug "p2 ------"
Debug a
Debug b
EndProcedure
Procedure p1()
Shared a
;Another possibility to make this code running "correct":
;Shared a,b
a.l=5 ; <-- new line
b.l=20
Debug "p1 ------"
Debug a
Debug b
p2()
EndProcedure
p1()
End
Since the first line is commented out, variable a is shared in both procedures p1 and p2, but not in the main program. Run the program and you will see that when the value of 5 is assigned to a in p1, a has the same value in p2. This is the same thing as using the statement "Shared a,b" (to share variable b) in procedure p1, as you show in your example.
I'm not sure if this explanation makes clear what I'm trying to say, so let me know if this makes sense.
Regards,
Eric
Shared have to be in main?
Posted: Mon Sep 15, 2003 11:07 pm
by Andy
Thank you Eric, for your anwser.
But - sorry - I do not understand

...
Also in your code will "a" be know in the main - or with other words, I can't use an "a" in the main, without that the "a" in p1() will not also change it... (I hope you can understand my terrible english

). The "b" is only known in p1() - not in p2() - despite the "shared a,b" command...
I think now, a better description will be, that "shared" is a kind of "global", but only for the procedure where "shared" is used. What do you mean?
Regards
Andy
Posted: Mon Sep 15, 2003 11:29 pm
by freak
> I think now, a better description will be, that "shared" is a kind
> of "global", but only for the procedure where "shared" is used. What do
> you mean?
Yes, shared makes a variable accessible in the main program, and in
all procedures, where this variable is declared as shared.
Timo