Observations about Global, Shared, and Protected

Everything else that doesn't fall into one of the other PB categories.
Tipperton
Addict
Addict
Posts: 1286
Joined: Thu Jun 19, 2003 7:55 pm

Observations about Global, Shared, and Protected

Post 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?
Last edited by Tipperton on Sun Jul 06, 2003 7:09 pm, edited 1 time in total.
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post 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
Tipperton
Addict
Addict
Posts: 1286
Joined: Thu Jun 19, 2003 7:55 pm

Post 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)
Last edited by Tipperton on Sun Jul 06, 2003 7:13 pm, edited 2 times in total.
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

Good thing you sorted it out! :D
Tipperton
Addict
Addict
Posts: 1286
Joined: Thu Jun 19, 2003 7:55 pm

Post by Tipperton »

Hey, I'm new to PureBasic ... what can I say... Except that I'm learning... :D
Post Reply