• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

About casting

ActionScript 2.0 lets you cast one data type to another. Casting an object to a different type means you convert the value that the object or variable holds to a different type.

The results of a type cast vary depending on the data types involved. To cast an object to a different type, you wrap the object name in parentheses (()) and precede it with the name of the new type. For example, the following code takes a Boolean value and casts it to an integer.

var myBoolean:Boolean = true;
var myNumber:Number = Number(myBoolean);

For more information on casting, see the following topics:

About casting objects

The syntax for casting is type(item), where you want the compiler to behave as if the data type of the item is type. Casting is essentially a function call, and the function call returns null if the cast fails at runtime (this occurs in files published for Flash Player 7 or later; files published for Flash Player 6 do not have runtime support for failed casts). If the cast succeeds, the function call returns the original object. However, the compiler cannot determine whether a cast will fail at runtime and won’t generate compile-time errors in those cases.

The following code shows an example:

// Both the Cat and Dog classes are subclasses of the Animal class
function bark(myAnimal:Animal) {
  var foo:Dog = Dog(myAnimal);
  foo.bark();
}
var curAnimal:Animal = new Dog();
bark(curAnimal); // Will work
curAnimal = new Cat();
bark(curAnimal); // Won't work

In this example, you asserted to the compiler that foo is a Dog object, and therefore the compiler assumes that foo.bark(); is a legal statement. However, the compiler doesn’t know that the cast will fail (that is, that you tried to cast a Cat object to an Animal type), so no compile-time error occurs. However, if you include a check in your script to make sure that the cast succeeds, you can find casting errors at runtime, as shown in the following example.

function bark(myAnimal:Animal) {
  var foo:Dog = Dog(myAnimal);
  if (foo) {
    foo.bark();
  }
}

You can cast an expression to an interface. If the expression is an object that implements the interface or has a base class that implements the interface, the cast succeeds. If not, the cast fails.

Note

Casting to null or undefined returns undefined.


You can’t override primitive data types that have a corresponding global conversion function with a cast operator of the same name. This is because the global conversion functions have precedence over the cast operators. For example, you can’t cast to Array because the Array() conversion function takes precedence over the cast operator.

This example defines two string variables (firstNum and secondNum), which are added together. The initial result is that the numbers are concatenated instead of added because they are a String data type. The second trace statement converts both numbers to a Number data type before performing the addition that yields the proper result. Data conversion is important when working with data loaded using XML or FlashVars, as shown in the following example:

var firstNum:String = "17";
var secondNum:String = "29";
trace(firstNum + secondNum); // 1729
trace(Number(firstNum) + Number(secondNum)); // 46

For more information on data conversion functions, see the entry for each conversion function in ActionScript 2.0 Language Reference in Flash Help: %{Array function}%, %{Boolean function}%, %{Number function}%, %{Object function}%, and %{String function}%.

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint