If you have multiple lines where you need to access properties of a
specific type then use 'as' and check for null. If it is one line then use
'is' and cast explicitly.

// use 'as' and check for null when multiple lines accessing a type are
involved:
public function doSomething(object:UIComponent):void {
    var button:Button = object as Button;
    var list:List = object as List;
    var checkbox:Checkbox = object as Checkbox;

    // universal UIComponent properties
    object.visible = true;
    object.includeInLayout = true;

    // button specified properties
    if (button) {
         button.label = "Button";
         button.emphasized = true;
    }
    else if (list) {
         list.labelField = "name";
         list.dataProvider = ['1','2','3'];
         list.selectedIndex = 0;
    }
    else if (checkbox) {
         checkbox.selected = true;
         checkbox.label = "checkbox";
    }
}


// use 'is' when casting involves a single line. not using 'as' to cast
though
// Button(object) is less typing than (object as Button)
// the (object is Button) already tells us it's a Button. no need for 'as'.
just cast as Button()
public function doSomething(object:UIComponent):void {

    object.visible = true;
    object.includeInLayout = true;

    if (object is Button) Button(object).label = "Button";
    else if (object is List) List(object).labelField = "name";
    else if (object is CheckBox) CheckBox(object).selected = true;

}

// use 'is' when checking for specific types and super types
public function checkIfToggle(object:UIComponent):void {

    if (object is ToggleButtonBase) {
          return true;
    }
}

result = checkIfToggle(new CheckBox()); // true
result = checkIfToggle(new RadioButon()); // true
result = checkIfToggle(new ToggleButton()); // true
result = checkIfToggle(new Button()); // false


On Mon, Feb 15, 2016 at 7:30 AM, suresh babu k s <sbab...@hotmail.com>
wrote:

> Hi,I would like to know best practice for the following approaches1)Doing
> 'is' check before casting data using 'as' operator   2)Casting data using
> 'as' operator and null testing laterplease share your views
>
>
>
> --
> View this message in context:
> http://apache-flex-users.2333346.n4.nabble.com/Best-Practice-is-as-and-null-tp11979.html
> Sent from the Apache Flex Users mailing list archive at Nabble.com.
>

Reply via email to