dispatchEvent tries to cast my customEvent to something completely different

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()[F:\Documenti\TSF\OnDe\Entertainment\TravelQuiz\WSConnector.as:58]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.rpc::AbstractService/dispatchEvent()[E:\dev\4.x\frameworks\projects\rpc\src\mx\rpc\AbstractService.as:333]
at mx.rpc.soap::WebService/http://www.adobe.com/2006/flex/mx/internal::wsdlFaultHandler()[E:\\dev\\4.x\\frameworks\\projects\\rpc\\src\\mx\\rpc\\soap\\WebService.as:296]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.rpc.wsdl::WSDLLoader/faultHandler()[E:\dev\4.x\frameworks\projects\rpc\src\mx\rpc\wsdl\WSDLLoader.as:105]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at HTTPOperation/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()[E:\\dev\\4.x\\frameworks\\projects\\rpc\\src\\mx\\rpc\\http\\HTTPService.as:989]
at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::faultHandler()[E:\\dev\\4.x\\frameworks\\projects\\rpc\\src\\mx\\rpc\\AbstractInvoker.as:350]
at mx.rpc::Responder/fault()[E:\dev\4.x\frameworks\projects\rpc\src\mx\rpc\Responder.as:68]
at mx.rpc::AsyncRequest/fault()[E:\dev\4.x\frameworks\projects\rpc\src\mx\rpc\AsyncRequest.as:113]
at DirectHTTPMessageResponder/errorHandler()[E:\dev\4.x\frameworks\projects\rpc\src\mx\messaging\channels\DirectHTTPChannel.as:405]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()

Thank You very much !

Wentu

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{ }