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.
Global vs. Shared variables
Global vs. Shared variables
Last edited by Tipperton on Mon Jul 07, 2003 1:28 pm, edited 1 time in total.
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:
Notice that the value of SValue can be read and set from the main program.
Regards,
Eric
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)
Regards,
Eric
Shared have to be in main?
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:
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
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
Best Regards
Andy
Andy,
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:
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
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.It seems to be, that shared variables >have< to be known in the main program:
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
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?
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
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

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
> 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
> 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
quidquid Latine dictum sit altum videtur