Skip to main content
Participant
March 2, 2011
Question

dispatchEvent tries to cast my customEvent to something completely different

  • March 2, 2011
  • 1 reply
  • 496 views

Hi All

I have a problem in dispatching a customEvent. I already managed to dispatch custom events in other projects and all worked well so i can't understand what's different now.

I have a WSConnectorEvent that extends Event

package
{
    import flash.events.Event;
    public class WSConnectorEvent extends flash.events.Event
    {
        public static const WSLOAD:String = "WSConnectorEvent_WSLOAD";
        public static const WSLOADFAULT:String = "WSConnectorEvent_WSLOADFAULT";


        public function WSConnectorEvent(type:String,bubbles:Boolean,cancelable:Boolean)
        {
            super(type,bubbles,cancelable);
        }
        override public function clone():Event
        {
            return new WSConnectorEvent(type,bubbles,cancelable);
        }
    }

}

The class WSConnector tries to reach a WebService and in the case of success or failure should send a WSConnectorEvent of type WSLOAD or WSLOADFAULT.

package
{
    import mx.rpc.soap.*;
    import mx.rpc.events.*;
    import mx.rpc.AbstractOperation;
    import flash.utils.getQualifiedClassName;
    import flash.events.*;
    import flash.events.EventDispatcher;

    public class WSConnector extends EventDispatcher
    {
        private static var instance:WSConnector = new WSConnector();
        private static var WSURL:String;
        private var quizWebService:WebService;
        private var serviceOperation:AbstractOperation;
        public function WSConnector()
        {
               //...
        }


        public function SetupWebService():void
        {
            quizWebService = new WebService();
            //movieWebService.loadWSDL(WSURL);
            quizWebService.addEventListener(LoadEvent.LOAD, quizWebService_onLoad);
            quizWebService.addEventListener(FaultEvent.FAULT, quizWebService_onFault);
            quizWebService.loadWSDL(WSURL);
        }

        function quizWebService_onLoad(evt:LoadEvent)
        {
            var wsce:WSConnectorEvent = new WSConnectorEvent(WSConnectorEvent.WSLOAD, true,true);

            dispatchEvent(wsce);
        }

       
       function quizWebService_onFault(evt:FaultEvent)
        {
            var wsce:WSConnectorEvent = new WSConnectorEvent(WSConnectorEvent.WSLOADFAULT, false ,false);

            dispatchEvent(wsce);
        }

       
    }
}

-----------------------------------------

what actually happens is that (in both cases) the variable wsce of WSConnectorEvent type is correctly created but when the dispatchEvent(wsce) is called, i receive the following error (in case of function quizWebService_onFault(evt:FaultEvent) ).


Question: why on hell is it trying to convert my custom event in a mx.rpc.events.FaultEvent ???? Isn't DispatchEvent accepting Event as a class ? Why does it need to cast it to something completely unrelated ???

TypeError: Error #1034: Assegnazione di tipo forzata non riuscita: impossibile convertire WSConnectorEvent@1f7efa1 in mx.rpc.events.FaultEvent. (in english this approximately means Impossible to cast from WSConnectorEvent@1f7efa1 to mx.rpc.events.FaultEvent)
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at WSConnector/quizWebService_onFault()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.rpc::AbstractService/dispatchEvent()
    at mx.rpc.soap::WebService/http://www.adobe.com/2006/flex/mx/internal::wsdlFaultHandler()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.rpc.wsdl::WSDLLoader/faultHandler()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at HTTPOperation/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()
    at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::faultHandler()
    at mx.rpc::Responder/fault()
    at mx.rpc::AsyncRequest/fault()
    at DirectHTTPMessageResponder/errorHandler()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()

Thank You very much !

Wentu

This topic has been closed for replies.

1 reply

JWentuAuthor
Participant
March 4, 2011

Thankx to maskedMan I now have the answer:

You're dispatching an instance of your custom event, but the function that you've set up to handle it expects a FaultEvent object.  In brief, the scenario looks something like this:
foo.addEventListener(CustomEvent.CUSTOMTYPE, handleEvent);
private function handleEvent(evt:FaultEvent):void{ }