Sunday, November 2, 2008

Verify if Object contains a proprety defined or not

In Adobe flex, you want to check if a property is defined inside an object, its pretty easy.
  /**
  * Test if the object obj contains a property named val
  *
  **/
  private function propertyTest(obj:Object):void
  {
     if(obj.hasOwnProperty("val"))
     {
       Alert.show('Has property named val defined and its value = ' + obj.val);
     }
     else
     {
       Alert.show('Property val is not defined in the object - obj');
     }
  }
  
  /**
  * Test method
  *
  **/
  private var testProperty():void
  {
     var val:String = "Test String";
     var obj:Object = new Object();
     obj.val = val;
 
     //test method call
     propertyTest(obj);
  }

1 comments:

Shoguren said...

Thanks! I was looking how to do exactly this!