Tuesday, November 18, 2008

How to test if an object is of a given type?

When dealing with typeCasting its essential to test if the objects are compatible before having a
typecast statement.Forgetting to do this results in fragile code that might throw a type cast error anytime.
Here is an example that shows you how to test this.
Note:A child is a type of parent and the vice versa is not true.

<?xml version="1.0" encoding="utf-8"?>



xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">



import mx.controls.Alert;

import mx.events.CollectionEvent;

import mx.collections.ArrayCollection;

private function test():void {

var parent:Parent = new Parent();

var child:Child = new Child();

if (child is Parent) {


} else {








Bookmark and Share

No comments: