RSS

How Clone() method works and Why to Write it?

30 Mar

Small round up to Events basic, I think that is important  :)

As we know “Event” class is used as the base class for the creation of Event objects, which are passed as parameters to event listeners when an event occurs. So to make avail the Event Object and its properties we always extends Event

Basically in Event class(flash.events.Event class) there is one method called as clone()

How clone() method works?
clone() method duplicates an instance of an Event subclass. Returns a new Event object that is a copy of the original instance of the Event object.

Why To write clone() method?

When creating your own custom Event class, we must override the inherited Event.clone() method in order for it to duplicate the properties of your custom class.

What if I don’t override clone() method?

This works fine if implementation doesn’t re-dispatch the event, but as soon as we re-dispatch the event (by calling dispatchEvent(event) in a handler that is handling event), we get a TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::Event@28ef5b1 to events.

Lets Produce the error :)

Main Application Code

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*" creationComplete="onCreationComplete()">
	<mx:Script>
		<![CDATA[
			import events.DemoCloneImportance;
			import mx.controls.Alert;
			private function onCreationComplete():void
			{
				demoCloneDispatcher.addEventListener(DemoCloneImportance.CLONE_EVENT,cloneDemoHandler);
				this.addEventListener(DemoCloneImportance.CLONE_EVENT,cloneRedispatchHandler);
			}
			
			private function cloneDemoHandler(event:DemoCloneImportance):void
			{
				Alert.show('In Dispatch Handler');
				this.dispatchEvent(event);		
			}
			
			private function cloneRedispatchHandler(event:DemoCloneImportance):void
			{
				Alert.show('In Redispatcher Handler');
			}
			
		]]>
	</mx:Script>

	<local:CloneInvokerComponent id="demoCloneDispatcher"/>
	
</mx:Application>

Component Class is as follows:


<?xml version="1.0" encoding="utf-8"?>
<!--CloneInvokerComponent.mxml-->
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300">
	<mx:Script>
		<![CDATA[
			import events.DemoCloneImportance;
		]]>
	</mx:Script>
<mx:Metadata>
	[Event(name="CloneEventDemo", type="events.DemoCloneImportance")]
</mx:Metadata>
	<mx:Button label="Dispatch Event" id="btnDispatch" click="dispatchEvent(new DemoCloneImportance(DemoCloneImportance.CLONE_EVENT,true))"/>
</mx:VBox>

Wrong Implementation of Event Class, (Without overriding clone() method )Must avoid it :)

package event
{
import flash.events.Event;

public class DemoCloneImportance extends Event
{
public function DemoCloneImportance(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}

public static const CLONE_EVENT:String = 'CloneEventDemo'		// Override the inherited clone() method.

}

}

Run the Application and we get Runtime error (Visible if Flash Debugger installed)

Now To Resolve it make Custom Event class implementation right :)

package event
{
import flash.events.Event;

public class DemoCloneImportance extends Event
{
public function DemoCloneImportance(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}

public static const CLONE_EVENT:String = 'CloneEventDemo'		// Override the inherited clone() method.

override public function clone():Event {
return new DemoCloneImportance(type, true,false);
}

}

}

This is something very basic but i think most important also :)

Best Luck & Happy coding :)

Thanks and regards

Shashank Kulkarni

Feel free to comment on the post :)

Advertisement
 

About shashankkulkarni

@Flex ,Fully loaded. Internet surfer, blogger.Post graduate from India
5 Comments

Posted by on March 30, 2010 in Flex(Amazing Errors)

 

5 Responses to How Clone() method works and Why to Write it?

  1. Jani Basha

    July 20, 2011 at 8:16 am

    Super example…. i like this… :)

     
  2. ajay

    July 26, 2011 at 9:28 am

    good example… really helped me a lot.

     
    • shashankkulkarni

      August 30, 2011 at 5:30 am

      Thanks Ajay for visiting. Hope it will help others too.

       
  3. Dhanraj

    September 15, 2011 at 7:03 am

    Nice example, it helps me a lot, thank you

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.