In general you always want to cast something like this - because 
event.currentTarget is of type object and NOT UIComponent or Button. But since 
objects are loosely typed as type Object, then you reference any property or 
method you want at compile time. Often at runtime though you will hit errors 
unless it is properly casted. Your sample application was done in Flex 2 right?
 
Definitely a best practice to be casting generic objects like this to the type 
of object you want to be working with to avoid runtime errors
 
Karl
Cynergy

________________________________

From: [email protected] on behalf of jairokan
Sent: Tue 2/27/2007 11:53 AM
To: [email protected]
Subject: [flexcoders] Basic inheritance question



Hi,
I'm reading the Developer's Guide and in the section using Events, they say on 
a paragraph that :
"If you try to call another method on the currentTarget (for example, the 
setStyle() method), Flex returns an error. The setStyle() method is defined on 
UIComponent, a subclass of DisplayObject. Therefore, you must cast 
currentTarget to UIComponent before calling the setStyle() method"
for example:
<mx:Script>
<![CDATA[
     import mx.core.UIComponent;

     private function myEventHandler(e:Event):void {
        UIComponent(e.currentTarget).setStyle("color", "red");
     }
  ]]>
</mx:Script>

and e.currentTarget is of type Button. But Button extends UIComponent, so we 
don't have to upcast the reference because setStyle method is public and is 
available to Button object reference0. I tried the code without upcasting and 
it works.
Is the writer wrong or Am I missing something on the concept.

Thanks

Jairo

 

 

Reply via email to