Is the canvas the only way to draw?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Paul
Posts: 195
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada
Contact:

Re: Is the canvas the only way to draw?

Post by Paul »

Maybe someone has written a number of useful Procedures and saved them as an Include file which you include in your app.
If you've gone and declared all your variables as Global, this would cause havoc if any of the included Procedures contain the same variable names so by using Protected, even if you have made all your variables Global there will be no mix ups.

Normally the variables inside a Procedure are in their own scope but if you use Global, they are exposed.

Code: Select all

x=10
Procedure Test()
	x=999
	Debug x
EndProcedure

Debug x
Test()
Debug x
The results will be 10, 999, 10


Now declare x as Global...

Code: Select all

Global x
x=10
Procedure Test()
	x=999
	Debug x
EndProcedure

Debug x
Test()
Debug x
You will see that the value of x now carries over into your main code: 10, 999, 999

Now make x Protected...

Code: Select all

x=10
Procedure Test()
	Protected x
	x=999
	Debug x
EndProcedure

Debug x
Test()
Debug x
Now you see that even though x has been declared as Global, it is not affected by the value of x in the Procedure: 10, 999, 10
User avatar
Random Terrain
Posts: 63
Joined: Fri Jul 09, 2021 9:48 pm
Location: USA
Contact:

Re: Is the canvas the only way to draw?

Post by Random Terrain »

Thanks. That makes sense.

I was thinking if you were making a game and different procedures added to the score, in that case you wouldn't want to protect that variable.
Post Reply