24 October 2011
You should be familiar with ActionScript 3 syntax.
Beginning
Computers work by storing and operating on values in memory. While computers may be good at keeping track of an individual location storing one value or another, humans are not. Instead of keeping track of the location of these items in memory, developers assign a name to reference each unique thing stored. This name is called a variable. Any time the computer sees a variable name in your code, the computer looks in its memory and uses the value it finds there. For example, if you have two variables named firstVariable and secondVariable , each containing a number, to add those two numbers you write the statement:
firstVariable + secondVariable
If firstVariable contained 5 and secondVariable contained 43, the arithmetic above results in 48.
A memory location can only hold one value at any given time. Therefore, a variable only refers to one value at any given time. That value can vary as your program runs. If secondVariable changes to 23, summing the two variables would result in 28.
In this article, you will learn about the characters that can be legally used to name a variable. You will also learn to declare, assign a value to, and control access to your variable. Finally, you will learn about another way to reference memory that is not variable, but rather constant.
Each variable must have a unique name. In ActionScript 3, you can name variables using words that remind you what the variables are. Once you properly name a variable, you only have to call the variable by name when you want to use it.
Each programming language has its own rules about naming variables. For example, some languages limit name length or prohibit certain characters or words. If you break one of these rules, an error occurs and your code does not run.
_ and $ in certain situations)In addition to observing the limitations of the language, programmers also follow conventions when naming their variables. Some conventions are considered standard practices within a professional community and therefore have shared meaning. Managers, development teams, or clients sometimes dictate conventions. Programmers also develop naming conventions as part of their own personal coding style. Unlike failure to observe the naming limitations of a language, failure to follow conventions does not prompt errors. However, following naming conventions can actually be more important to a successful development process than observing the naming limitations.
totalCost or dailyTotalsArray_ ) only to start the name of a private variable$ ) sign only to start the name of a static variableYou control access to variables, classes, functions, and methods using access control attributes. Access control attributes help achieve encapsulation. Encapsulation is an important concept in object-oriented programming; for more information, see Object-oriented programming concepts: Encapsulation.
You use these access control attribute keywords when you declare variables to control access in ActionScript 3.
Table 1. Access control attribute keywords
Access control attribute keyword |
Access |
available to any caller |
|
available only to the class that defines it |
|
available only to the class that defines it and to any subclasses of that class |
|
available to any caller within the same package |
Creating a variable is called variable declaration. ActionScript 3 requires the use of a keyword— var —to signal that you are declaring a variable. Including the keyword var , there are four parts to (non-local) variable declaration in ActionScript 3:
varHere are some examples of variable declaration:
public var userName:String;
private var _correctAnswer:Boolean;
internal var userAge:uint;
protected var predictedTemperature:int;
private var _productPrice:Number;
For more information on data types, see ActionScript 3 fundamentals: Data types.
Putting data into a variable is called assignment. It uses the assignment operator = . Use the assignment operator with a variable name on the left and the data on the right. You can assign data anytime after a variable has been declared.
The following example declares a variable named userAge to hold an integer and then assigns it the value of 21.
internal var userAge:int;
userAge = 21;
Because you can assign data anytime after a variable has been declared, you can do it immediately after declaration within the same statement.
public var userName:String = "Jane";
private var _correctAnswer:Boolean = true;
internal var userAge:uint = 19;
protected var predictedTemperature:int = -5;
private var _productPrice:Number = 5.99;
Assignment can take place anytime after a variable has been declared—at declaration, many lines later, or after an event occurs. Assignment takes place when the data is known.
Constants are properties with a fixed value that cannot be altered. You can assign a value to a constant only once, and the assignment must occur in proximity to the declaration of the constant. An error results if you attempt to change the data in a constant.
Constants are declared using the const keyword instead of var . By convention, constant names are all caps with words separated by an underscore
Example: You are building an application that converts miles to kilometers. Miles are multiplied by a conversion value to get kilometers. If that conversion value changes, the conversions are no longer correct. Use a constant to hold the conversion value.
const MILES_KM_CONVERSION_VALUE:Number = 1.61;
Variables are a method of communication used to represent storage locations for various items in memory. They are a central concept in programming and are used throughout every other aspect of programming.
When variables are combined with functions, they become parameters or local variables. When they are combined with objects, they can represent properties or data which an object stores or they can serve as a reference to the object itself. To learn more about each of these: ActionScript 3 fundamentals: Functions and Introduction to Objects (coming October 31).
The content in this article is based on material originally published in the Learning ActionScript 3 user guide created by Adobe Community Help and Learning.