A very common concept that is found in most scripting and proŽgramming languages is that of the variable. This can be considŽered as a container that holds data and can be used later on when necessary. In Flash ActionScript a letter or any particular word is assigned to identity such a variable. The user can also assign numeric values and characters to it. A variable is primarily used to store values, and the var statement is mainly used for declaring those variables. For example:
var x; // a variable x is declared
If the var statement is omitted while declaring a variable, a compilation error in strict mode and run-time error in standard mode will be displayed. For example consider the following statement:
i; // error will occur if i was not previously defined
On the other hand, when the user wants to declare a variable of a particular data type, sayan int, the following declaration must be given:
var x:int;
Besides this, a variable can also be assigned a value by using the (=) operator. For example, the following code declares a variable k and assigns the value 20 to it:
var k:int; k = 20;
The user can also declare and initialize an array or instantiate an instance of a class. The following code shows an array that is declared and is also assigned relevant values.
var numExample = [ "zero", "one", "two"];
Besides the above method, you can also create an instance of class with the help of the new operator. The following code create an instance of a named Customized Class, and assigns a referenc to the newly created class instance to the variable name customerItem:
var customerItem:CustomizedClass = new CustomClass() ;
While deClaring more than one variable, you can declare the all in a single line of code with the help of the comma (,) operato and assign values to them as shown below:
var x:int = 30, y:int = 20, z:int =30;
Understanding variable scope:
The scope of a variable is a description of the accessibility of tha variable. There are mainly two types of variables that are used i Flash ActionScripting. One is the global variable that is accessibl from anywhere in the entire code; while the local variable is on that is defined in only one part of the code, and only accessibl from there. A global variable can also be defined outside any fun tion. The following code shows the definition of a global variabl available from both inside and outside of a function.
var strGlobal: String = "INA";
function scopeTestFinal()
{
trace (strGlobal) ; // Global
}
scopeTestFinal();
trace(strGlobal); // Global
If the variable name that is used for a local variable is alread declared as an global one, the global definition is hidden by the local definition while the local variable is in scope. Unlike in other programming languages (C or C++), the variables used in Action Script do not have a block-level scope. A variable declared inside a block of code is accessible not only to that block but also to any other parts of the function related to that block. The following code snippet contains variables that are defined in various block scopes. All the variables are available throughout the function.
funcation blockTest ( testingArray:Array)
{
var numElement:int = testingArray.length;
if (numElement > 0)
{
var elemStr:String = "Element #";
for (var i:int = 0; i < numElement; i++)
testingArray [i] ;
trace(elemStr + valueStr);
}
trace (elemStr, valueStr, i); still defined
trace (elemStr, valueStr, i); // all defined if numElements > 0
Default Values
The default value of a variable is said to be that value which a variable contains before a new value is assigned to it. A variable declared but without a value is said to be in an uninitialised state whose value further depends on its data type. The table provided below gives the default values of variables organised according to their data types.
![]()



Reply With Quote
Copyright Techfuels
Bookmarks