Assigning a data type

You need to assign data types whenever you define a variable, whether you declare a variable using the var keyword, create a function argument, set function return type, or define a variable to use within a for or for..in loop. To assign a data type, you use post-colon syntax, which means you follow the variable name with a colon and then the data type:

var my_mc:MovieClip;

There are many possibilities for data types, ranging from the native data types such as Number, String, Boolean, or built-in classes that are included in Flash Player 8, such as BitmapData, FileReference, or even custom classes that you or other developers have written. The most common types of data types you might need to specify are the built-in data types such as Number, String, Boolean, Array, or Object, which are shown in the following code examples.

To assign a specific data type to an item, specify its type using the var keyword and post-colon syntax, as shown in the following example:

// Strict typing of variable or object
var myNum:Number = 7;
var birthday:Date = new Date();

// Strict typing of parameters
function welcome(firstName:String, age:Number) {
}

// Strict typing of parameter and return value
function square(myNum:Number):Number {
  var squared:Number = myNum * myNum;
  return squared;
}

You can declare the data type of objects based on built-in classes (Button, Date, and so on) as well as classes and interfaces that you create. In the following example, if you have a file named Student.as in which you define the Student class, you can specify that objects you create are of type Student:

var myStudent:Student = new Student();

For this example, suppose you type the following code:

// in the Student.as class file
class Student {
  public var status:Boolean; // property of Student objects
}
// in the FLA file
var studentMaryLago:Student = new Student();
studentMaryLago.status = "enrolled"; /* Type mismatch in assignment statement: found String where Boolean is required. */

When Flash compiles this script, a type mismatch error is generated because the SWF file expects a Boolean value.

If you write a function that doesn't have a return type, you can specify a return type of Void for that function. Or if you create a shortcut to a function, you can assign a data type of Function to the new variable. To specify that objects are of type Function or Void, see the following example:

function sayHello(name_str:String):Void {
    trace("Hello, " + name_str);
}
sayHello("world"); // Hello, world
var greeting:Function = sayHello;
greeting("Augustus"); // Hello, Augustus

Another advantage of strict data typing is that Flash automatically shows code hints for built-in objects when they are strictly typed. For more information, see About assigning data types and strict data typing.

Files published using ActionScript 1.0 do not respect strict data typing assignments at compile time, so assigning the wrong type of value to a variable that you have strictly typed doesn't generate a compiler error.

var myNum:String = "abc";
myNum = 12;
/* No error in ActionScript 1.0, but type mismatch error in ActionScript 2.0 */

The reason for this is that when you publish a file for ActionScript 1.0, Flash interprets a statement such as var myNum:String = "abc" as slash syntax rather than as strict typing. (ActionScript 2.0 doesn't support slash syntax.) This behavior can result in an object that is assigned to a variable of the wrong type, causing the compiler to let illegal method calls and undefined property references pass through unreported.

Files published using ActionScript 2.0 can optionally use data typing. Therefore, if you implement strict data typing in your code, make sure you set your publish settings to ActionScript 2.0. You can specify the publish settings and define which version of ActionScript you want to publish your files as by modifying the publish settings from the main menu (File > Publish Settings) or by clicking the Settings button in the Property inspector (make sure no instances are selected). To use a specific version of ActionScript or the Flash Player, select the Flash tab in the Publish Settings dialog box, and make a selection from the ActionScript version pop-up menu.

For information on type checking, see About type checking.


Flash CS3