Page 1 of 1

Observations about Global, Shared, and Protected

Posted: Sun Jul 06, 2003 6:22 pm
by Tipperton
It seems to me that the place to use Global or Shared is in procedures rather than in the main code.

This works:

Code: Select all

a.s

Procedure MyProc()
  Global a.s
  a="somethingnew"
EndProcedure

a="something"
Debug a
MyProc()
Debug a
End
This doesn't

Code: Select all

Global a.s

Procedure MyProc()
  a.s="somethingnew"
EndProcedure

a="something"
Debug a
MyProc()
Debug a
End
Neither does this:

Code: Select all

a.s

Procedure MyProc()
  a.s="somethingnew"
EndProcedure

a="something"
Debug a
MyProc()
Debug a
End
This brings to mind, "What good is Protected then?" since it appears that variables defined in a procedure default to Protected anyway.

Thoughts?

Posted: Sun Jul 06, 2003 6:34 pm
by Pupil
Maybe you should call the procedure?? ;)
Your code snipps works exactly like i expected them to do. Shared are supposed to be declared inside a procedure, globals can be declared mostly anywhere as long as they are declared on a line above the first time the global variable is used in the code..

Code: Select all

Global a.s

Procedure MyProc()
  a.s="something"
EndProcedure

Debug a
MyProc()
Debug a

Posted: Sun Jul 06, 2003 7:04 pm
by Tipperton
Pupil wrote:Maybe you should call the procedure??
The assumption is that at some point further on the procedure IS called...

OK, I figured out what I was doing wrong with global...

Code: Select all

XInclude "File.pb"

Global a.s
Should have been

Code: Select all

Global a.s

XInclude "File.pb"
File.pb contains the procedure that modifies a... Ooops! :oops:

Now Protected makes sense too... 8)

Posted: Sun Jul 06, 2003 7:10 pm
by Pupil
Good thing you sorted it out! :D

Posted: Sun Jul 06, 2003 7:14 pm
by Tipperton
Hey, I'm new to PureBasic ... what can I say... Except that I'm learning... :D