ActionScript Syntax for Flex Applications - Variables and Properties (Page 3 of 4 )
A variable is a named element you can use to store data or a reference to data. You can assign values to and read values from a variable.
When you want to work with a variable, the first thing you’ll need to do is declare it. Declaring a variable allocates memory for it and tells the application that the variable exists. You can declare a variable using thevarkeyword as follows:
var variableName;
Thevarkeyword is followed by the name of the variable. Variable names in ActionScript are arbitrary, but they must follow a few simple rules:
The variable name can consist only of letters, numbers, dollar signs, and underscores.
The variable name must not start with a number.
By convention, all ActionScript variables use initial lowercase characters rather than initial uppercase characters. The following declares a variable calleduserName:
var userName;
Although you can declare a variable without a data type, it’s always recommended that you declare a variable with a data type. You can add a data type to the variable declaration using post-colon syntax as follows:
var variableName:DataType;
The data type determines the kind of data you can store in the variable. There are many data types, ranging from simple strings and numbers to reference types (such as arrays), and all the types defined by the Flex framework (e.g.,TextInput,Button, etc.). There are far too many data types to list comprehensively here (especially since you can define custom data types). However, some of the most common core data types areString,Number,int,uint,Boolean,Date, andArray, as defined in Table4-1 .
Table 4-1. Common data types and their descriptions
Data type
Description
String
One or more characters, including all Unicode characters
Number
Any numeric value, including floating-point numbers
int
Positive and negative integers and 0
uint
Positive integers and 0
Boolean
True or false
Date
The date and time
Array
An index-ordered collection of data
When a variable is declared with a data type, you’ll receive errors if you attempt to assign an invalid value to the variable. Flex applications provide both compile-time and runtime type checking.
The following example declares theuserNamevariable with the data typeString:
var userName:String;
Once you’ve declared a variable, the next thing to do is assign values using an assignment operator (an equals sign), as in the following example:
userName = "Flex User";
You can also combine a declaration and assignment into one line:
var userName:String = "Flex User";
When you want to retrieve a value from a variable, you simply reference the variable in a statement or expression that expects that type of value. The following example assigns the value from theuserNamevariable to thetextproperty of atextinput component:
textInput.text = userName;
Variables are placed within class methods (find more on method syntax in the “Methods section, later in this chapter). Variables declared outside of methods are called properties, and they are scoped to the entire class. In most respects, variables and properties are the same. However, there is one key difference that shows up syntactically, which is simply a matter of scope. Here we describe the contrast between variable and property scope:
All variables declared within methods are scoped exclusively to those methods. That means you cannot reference a variable outside the method in which it is declared.
Properties, on the other hand, have much greater scope. At a minimum, a property is accessible within the entire class. However, you can also opt to allow the property to be accessible outside the class with various settings called modifiers.
Classes define properties using quite a few possible modifiers. A property can be one of the following:
public Thepublicmodifier means the property is accessible outside the class (e.g., from an instance of the class).
private Theprivate modifier makes the property accessible only within the class.
protected Theprotectedmodifier makes the property accessible only within the class and its subclasses.
internal Theinternalmodifier makes the property accessible only within the package.
Practically, you should always declare properties asprivateorprotected. It is not a good idea to declarepublicproperties because a class should always manage its state (the values of its properties).internalproperties are a bad idea for the same reason.
You can declare properties in much the same way as you would declare variables: using thevarkeyword. In addition, a property name must follow the same rules as variable names. A common convention (and one used by this book) namesprivate andprotected properties with an initial underscore (_) to help distinguish them from local variables declared within methods. The following example declares aprivate property called_loader of typeURLLoader:
package com.example{ import flash.net.URLLoader; import flash.net.URLRequest; public class Example { private var _loader:URLLoader; } }