Programming Primer
Licensing And Preface

Standardized Coding Practices

Errors And Exceptions


String DataType

Number DataTypes


Commonly Used Flash Commands

More Topics:





Part 4: Variables

What are Variables?

Variables create a means of storing a value under a name and provide us, through this name, the ability to reference this value (which may be a simple number or a complex expression) and use this value in an expression.

Variables are created in the client computer's memory and are stored with their associated value. It is far easier to reference a value using it's variable name than to know exactly where this value exists in a client computer's memory and refer to it as such.

Lets take a real world example to demonstrate how we use variables in programming, say you had a cell phone with your friend's name and number on it. Your friends name is “john”, this is the variable name. His cell number is “+27829662141”, this is the variable's value. If you were to call your friend on his cell phone you do not have to know where he is in order to get a hold of him. In the same sense you don't need to know where in your computer's memory a variable's value is stored, you just refer to the variable's name to get it's value. As you can imagine this makes things a lot easier. In the same way referring to a value through it's variable name will make scripting a lot easier especially when your variable has a constantly changing value assigned to it.

Variables, as the name implies do not have to be constant in their value. Their value can change at any point. What cannot change about a variable is the way you refer to it. A variable's name must always remain consistent, there is no way of changing a variable name however you can create a new variable with a different name and assign it the value of your previous variable via it's name. To elaborate on the previous “real world” example of your friend and his cell phone number. The person you know as “john” will always be “john” however his cell phone number (particularly if he's south African) may change from time to time, that is to say his name (or the way you refer to your variable) stays the same but his number (or the value assigned to the variable) is prone to change.

Creating Variables

The process of creating a variable is generally referred to as declaring a variable. The best way to declare a variable in Flash is to assign your variable a name, data type it and if necessary assign a value to it. For example,

var friendName:String = 'john';

This is referred to as an assignment statement, as we are assigning the value that is on the right of the equals sign to whatever is on the left of the equals sign.

Using variables can save us from a lot of unnecessary typing. Variables can have any value assigned to them you like they can have a simple string like “john”, a number or a complex expression assigned to them. Here's an example of how to use a variable within a trace statement,

trace(“Hi, my name is” + friendName + “!”);

Following from our last example Flash would print the following string in the Output window,

Hi, my name is john!

Creating Legal Variable Names

Variable names can only contain alphanumeric characters and underscores.
Variable names cannot start with numbers.
Variable names should be descriptive but not verbose.
Use consistent casing. Variable names generally do not start with an uppercase character unless the variable is assigned the value of an instantiated object.
Your variable names should be self documenting, that is to say they should be descriptive about their purpose without having to include additional comments explaining their purpose.
Variables that are typed in all uppercase characters are referred to as constants. Their value is not supposed to change. This is simply a naming convention and there is nothing stopping you from changing the value of a constant in Flash, although this is not a recommended workflow. So beware when using this naming convention as it could lead to confusion if not used properly.

Next: Strings Data Type