Home‎ > ‎Variables‎ > ‎

Variables - Part 2 (Advanced)

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.

(Please review the basics of variables at Variables before continuing)

You create a variable just by putting something into it

You can save information that a user gives you:
    ask "What is your name?"
    put it into tName

You can save information from a card:
    put field "address" into gAddress

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. 

You declare a variable as either:
    local - used only in this handler, on this object or on this card
    global - saved permanently and can be used anywhere

Examples:
    the following declares variable which are only good in the place they are declared:

        local gName
        local lAddress, lCity, lStste, lZip

    the following declares Global (permanent) variables:

       global gName
       global gAddress, gCity, gState, gZip

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. 

If you remember, to change a property, you use the "set" command
e.g.
        set the loc of button "box" to 100,245
        set the visible of image "Spider.jpg" to false
        set the length of graphic "Square" to 200

You do the same with "custom" properties. If you use a property name that does not exist, then LiveCode will create one with that name. (just like it does with variables)

        set the abc of button "box" to "no"

    now button "box" has a custom property "abc" with "no" in it

    to get it, you use the "put" command or the "get" command:

        put the abc of button "box" into x
        if x is "no" then
            ...
        end if

    or use the "get" command that gets it into the variable "it"
        get the abc of button "box" 
        if it is "no" then
            ...
        end if
        
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)


Comments