Although the two statements above may appear algebraically equivalent, they are not programmatically equivalent. See Chapter 5. There are four main storage classes although parameters are generally treated as local variables , each of which is created in a different way.
Local variables or temporary variables are fleeting; they come into existence when they are first assigned a value, and they disappear at the end of the current handler. Use local variables for temporary needs that are confined to the current handler. To create a local variable, pick an arbitrary name, and assign a value to it. Variables such as i , j , and k are commonly used for loops or indices.
Variables such as x and y are commonly used for coordinates. In this example, i and y are local variables everything else is a reserved Lingo keyword. The showLocals command must be used from within the handler for which you wish to display local variables.
Likewise, you cannot use put from the message window to display a local variable. Local variables are independent of other variables in other handlers that may have the same name. Because they cannot be used until they are assigned a value, local variables have no default value. Using one before assigning it a value results in a " Variable used before assigned a value " error.
In this example, x is an uninitialized local variable and will cause an error. See " Special Treatment of the First Argument Passed " later in this chapter for an explanation of why using an undeclared local variable as the first argument to a function does not generate an error.
Parameters are local variables that automatically receive the value s of incoming arguments used in the call to the handler see " Parameters and Arguments " and " Generalizing Functions " later in this chapter. Parameters are declared named on the same line as the handler name. Parameters can assume different values each time a handler is called. A different copy of the parameters is created each time the handler is called and disappears when the handler ends.
Changes to parameters within a handler generally have no effect outside that handler, but modifying a Lingo list passed as a parameter will modify the original list in the calling routine as well. See Chapter 6 , Lists for important additional details. Global variables or simply globals are declared using the global keyword, and they persist throughout all movies until your Projector quits.
They come into existence when a handler that declares them as global first runs and can be accessed by any handler that also declares them as global. Global variables can be displayed in the Message window using showGlobals the built-in global variable version always appears in the list of globals.
Use clearGlobals to reset all global variables to VOID , except for the version global, which cannot be cleared. ClearGlobals also clears the actorList of the current movie in D6.
Globals can be shared by MIAWs and the main movie. Any change to the value of a global variable is reflected everywhere it is used. Shockwave clears global variables if the browser issues a Stop command. Globals are necessary when you want a variable to outlive the handler in which it is used or to send information between two handlers that are not otherwise connected. In this example, gOne and gTwo are global variables shared by two handlers. Lingo globals are declared, by convention, at the top of a handler immediately under the handler name; declaring globals in the middle of a handler is allowed but discouraged.
You can declare more than one global with the global keyword by separating the variables with commas, as shown above. You can instead use a new line for each global declaration, such as:.
Globals declared outside of a handler so-called " global " globals are treated as if they were declared within all subsequent handlers within the same script. For example, if the two handlers above are in the same script, the global declarations could be moved outside the handlers themselves and placed at the top of the script:.
Property variables are declared using the property keyword and persist as long as the object of which they are a property exists. See Chapter 12 , Behaviors and Parent Scripts, if you are not familiar with object-oriented programming, or just ignore this section for now.
Property variables are programmer-defined and should not be confused with the built-in Lingo properties, although both are attributes of their respective objects. Built-in Lingo properties are predefined attributes of built-in objects, such as sprites and cast members.
Property variables are programmer-defined variables that are used to add attributes to their own objects namely scripts. Property variables are instantiated created when the parent script or Behavior that declares them is itself instantiated either by Director or by the programmer. For example, when Director encounters a Behavior attached to a sprite in the Score it instantiates that Behavior and its properties.
This is explained in detail in Chapter For now, just assume that when a Behavior is encountered in the Score, Director assigns appropriate values to any property variables that the Behavior declares. Properties can then be accessed by any handler within the same script. Each instance use or occurrence of the parent script or Behavior gets its own copy of the property variables that are independent of other copies despite having the same name , just as all sprites and cast members have independent properties named width and height.
Property variables are declared at the top of the parent script or Behavior before the first handler. You can declare multiple property variables, separated by commas, using one property statement, or you can use separate property statements for each property variable. When a handler inside a parent script or Behavior is called, its private copies of those property values are used, unlike global variables that are shared among all scripts. Property variables must be declared outside of any handlers in the script, as shown in the example below.
To test it in the Message window, first instantiate the parent script by calling the new handler. When the programmer instantiates a script, he customarily specifies initial values for the properties by specifying them as arguments to the new handler although some programmers prefer to assign properties as a separate step from instantiating a script.
We then create a second instance with different values to be used as pOne and pTwo. We pass an instance created using new to showProps either instance1 or instance2. That allows showProps to determine the correct properties for each instance separately. Note how the results printed by showProps depend on which instance variable we pass to it. Note that inside the script that declares them, property variables are accessed by using their name, as shown in showProps above.
Programmer-defined property variables can also be accessed from outside a script instance using the script instance and the keyword the , such as:. Property variables belonging to scripts can also be accessed by referencing the script itself rather than an instance of the script, such as:. Although they can also be accessed using the keyword the , remember that property variables are programmer-defined and are not the built-in Lingo properties that also happen to start with the keyword the.
Continuing the example above, you can access the properties of instance1 and instance2 without using showProps , instead using:. Note that simply typing put pOne or put the pOne in the Message window would fail because you must specify the script instance that owns the property. If no script instance is specified, the property name must be a built-in system property, such as:. Refer to Chapter 12 , especially if this section left you thoroughly confused. The following are the most common errors of both new and experienced programmers when using variables.
Attempting to use a variable that has never been declared or assigned a value causes a " Variable used before assigned a value " error, such as:.
Global variables can be declared with the global keyword, without necessarily being assigned a value we presume the global was assigned a meaningful value elsewhere; if not, it defaults to VOID :.
In the following statement, Director complains about the undeclared local variable y , which we are attempting to use in the expression although it has not been previously assigned a value.
Director does not complain about the new local variable x , to which we are attempting to assign a value. In other words, the right side of the equation can use only existing variables, but the left side of the equation can be either an existing or a new variable. Use property variables for attributes that have a different value for each instance of a parent script or Behavior or that must persist for the life of the object.
Use global variables when a value must outlive the handler, object, or movie in which it is used or must be accessible to multiple handlers, objects, or movies.
Use local variables for values that are used only for convenience within the current handler and then discarded, such as indices in a repeat loop or interim steps in a mathematical calculation. Use parameters to accept inputs that can make a handler more flexible see " Parameters and Arguments " later in this chapter. Most novices use either too many or too few variables. Use variables whenever you want Director to remember something, such as the results of calculations, user input, lists of items, or anything that you need more than once.
Programming is like cooking. You may be able to cook dinner in one pot, or you may need two frying pans and a pressure cooker; it depends on the recipe and your personal style. This example:. Both examples are equivalent, but the second one is somewhat easier to read and maintain because the result of the lengthy expression is stored in myLocH , which is then used for comparing and displaying the value. When you assign a variable, it records a snapshot in time.
The value keyword represents the value we assign to the property. Now we can use the Name property to access and update the private field of the Person class:. Try it Yourself ». The following example will produce the same result as the example above. The only difference is that there is less code:. We just launched W3Schools videos. Get certified by completing a course today! If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:.
If you don't fully understand it, take a look at the example below. This is particularly important in Objective-C because it is often by way of getters and setters that memory is managed e. Beyond a memory management strategy, the practice also promotes encapsulation and reduces the amount of trivial code that would otherwise be required. It is very common to declare an ivar in brackets and then an associated property as in your example , but that isn't strictly necessary.
Defining the property and synthesizing is all that's required, because synthesizing the property implicitly also creates an ivar. Again, this isn't necessary, but by defining the ivar next to the synthesizer, you are reducing the locations where you have to type the name of the ivar while still explicitly naming it.
When you define a property a getter and setter is created for you. When you access them using object. When you declare variable in interface setters and getters are not written for you. Important point unless you provide custom getters and setters. If you add any files,it will delete all existing files related to this question- questions only answer remains unchanged.
0コメント