Variables - Local, Global and Custom Properties Variables Think of a variable as a box where you can save information or store things. It allows you to save information from a field on a screen or card before it goes away, for calculations or just to use later. You save it using the "put" command. You create a variable just by putting something into it You can save information that a user gives you:
You can save information from a card:
Variables - Local vs Global Instead of just creating a variable "on the fly" by just putting something into it, it is better to Declare it first.
note: To use a global variable anywhere else, you have to declare/reference it in every place that you use it. If you use it in 3 different places, you have to declare it in each of those places. Custom Properties - an even better way to save/use information Custom properties are just like normal properties that belong to the object. They stay with the object and are there when you open it up again. The properties belong with the object not the program. Variables, even global ones, go away when the program quits. Properties and even custom properties, do not go away. They are there every time you open the program. They are good for remembering and saving information for when you open your program the next time. (note: This is not true of standalones because of the security restrictions of the operating systems (Windows, OSX, etc, you can not modify a running/installed program. But there are ways around this - see the lessons on standalones and custom properties) The other nice advantage with custom properties is that you have to explicitly change them with specific command. If you make a special handler to change to use to change it, it is easier to track changes to it. With global variables, anyone can access and change them. Often, code to change them are in multiple places. You do not always know who changed them or where they were changed. It could even have been a bug in your code. Variables are really hard to debug.
Custom Properties are Powerful You can save arrays, images, text, and almost anything in a custom property. Instead of writing data to a file and reading it back every time your program starts up, save the data in custom properties and it will always be there. Custom properties also provide a mechanism to create permanent, user-modifiable code. (See the lessons/examples on Student Apps - which is not yet available but should be in a week or two) |