title: Flex4 Modules / Passing Data
body: <p>I have a Flex 4 application that loads a module. Everything works fine except that I cant create and object or cast an object to a type of my module. At run time it throws an error.</p>
  
  <p>I'm not sure if there is an issue with modules, Flex4, and spark? I've tried many different ways to set/cast these objects, but nothing seems to work. </p>
  
  <p>I need to get these objects so I can pass data to the module. Thanks for any help.</p>
  
  <p>Main.mxml...</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                 xmlns:s="library://ns.adobe.com/flex/spark"
                 xmlns:esri="http://www.esri.com/2008/ags" 
                 xmlns:mx="library://ns.adobe.com/flex/mx" 
                 minWidth="955" minHeight="600" applicationComplete="init();"
                  &gt;
      &lt;fx:Declarations&gt;
          &lt;!-- Place non-visual elements (e.g., services, value objects) here --&gt;
      &lt;/fx:Declarations&gt;
  
      &lt;fx:Script&gt;
  
          &lt;![CDATA[
              import com.gisworkshop.widgets.DataFrame.DataFrame;
              import com.gisworkshop.widgets.DataFrame.IDataFrame;
  
              import mx.containers.Canvas;
              import mx.core.IVisualElement;
              import mx.events.ModuleEvent;
              import mx.modules.IModuleInfo;
              import mx.modules.ModuleLoader;
              import mx.modules.ModuleManager;
  
              public var module:IModuleInfo;
  
  
              public function init():void{            
  
                  /*  Use the Module Manager to add the module to the app.
                      We have to use the addElement method of the main application so that the componants in the Module are seen.
                      This is a little different from the documentation because we are using spark and Flex 4 in the application. */
  
                  module = ModuleManager.getModule("com/gisworkshop/widgets/DataFrame/DataFrame.swf");                
  
                  module.addEventListener(ModuleEvent.READY, modReadyHandler, false, 0, true);
                  module.addEventListener(ModuleEvent.ERROR, modErrorHandler, false, 0, true);
                  module.addEventListener(ModuleEvent.UNLOAD, modUnloadHandler, false, 0, true);
  
                  module.load(null, null, null, moduleFactory);
  
              }
  
              private function modErrorHandler( moduleEvent:ModuleEvent ):void
              {
  
  
              }
  
              private function modReadyHandler( moduleEvent:ModuleEvent ):void
              {
                  this.addElement(module.factory.create() as DataFrame);
  
                  var obj:DataFrame;
  
  
              }
  
              private function modUnloadHandler( moduleEvent:ModuleEvent ):void
              {
  
                  //When we unload the module, remove it from the application
                  for (var i:int = 0; i &lt; numElements; i++) {
  
                      var objModule:Object = this.getElementAt(i);
  
                      //the object we want to remove has the property "cvsDataFrame"
                      if (objModule.hasOwnProperty("cvsDataFrame")){
                          this.removeElementAt(i);
                      }   
  
                  }
  
                  /*When we unload the module, we remove all the event listeners and anything referenced to the module object.
                    Re-define the event listeners for the module object */
  
                  module = ModuleManager.getModule("com/gisworkshop/widgets/DataFrame/DataFrame.swf");                
  
                  module.addEventListener(ModuleEvent.READY, modReadyHandler, false, 0, true);
                  module.addEventListener(ModuleEvent.ERROR, modErrorHandler, false, 0, true);
                  module.addEventListener(ModuleEvent.UNLOAD, modUnloadHandler, false, 0, true);
  
                  module.load(null, null, null, moduleFactory);
  
              }
  
              private function loadModule():void{
  
                  module.unload();
  
              }
  
          ]]&gt;
  
      &lt;/fx:Script&gt;
  
      &lt;esri:Map id="mainMap"&gt;
  
          &lt;esri:ArcGISDynamicMapServiceLayer id="baseMap" url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer"/&gt;
      &lt;/esri:Map&gt;
  
      &lt;mx:Button label="Load" click="loadModule();"/&gt;
  
  &lt;/s:Application&gt;
  </code></pre>
  
  <p>DataFrame...</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;mx:Module xmlns:fx="http://ns.adobe.com/mxml/2009"
             xmlns:s="library://ns.adobe.com/flex/spark" 
             xmlns:mx="library://ns.adobe.com/flex/mx" layout="absolute" width="400" height="300" implements="com.gisworkshop.widgets.DataFrame.IDataFrame"
  
             &gt;
      &lt;fx:Declarations&gt;
          &lt;!-- Place non-visual elements (e.g., services, value objects) here --&gt;
      &lt;/fx:Declarations&gt;
      &lt;fx:Script&gt;
  
          &lt;![CDATA[
  
              import com.esri.ags.Map;
  
              [Bindable]
              private var _Map:Map;
  
              public function setMap(map:Map):void{
  
                  _Map = map;
  
              }
  
              public function zoom():void{
  
                  _Map.zoomIn();
  
              }
  
          ]]&gt;
  
      &lt;/fx:Script&gt;
  
      &lt;mx:Canvas id="cvsDataFrame" width="100" height="50" top="90" left="0" &gt;
          &lt;mx:Button label="Zoom" left="0" top="0" click="zoom();"/&gt;
      &lt;/mx:Canvas&gt;
  
  &lt;/mx:Module&gt;
  </code></pre>
  
  <p>IDataFrame interface...</p>
  
  <pre><code>package com.gisworkshop.widgets.DataFrame
  {
      import com.esri.ags.Map;
  
      import mx.core.IVisualElement;
  
      public interface IDataFrame 
      {
          function setMap(_Map:Map):void;
          //function 
      }
  }
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>ArgumentError: Error #2004: One of the parameters is invalid.
      at flash.display::Graphics/drawRoundRect()
      at spark.primitives::Rect/draw()[E:\dev\4.x\frameworks\projects\spark\src\spark\primitives\Rect.as:459]
      at spark.primitives.supportClasses::StrokedElement/updateDisplayList()[E:\dev\4.x\frameworks\projects\spark\src\spark\primitives\supportClasses\StrokedElement.as:143]
      at spark.primitives.supportClasses::GraphicElement/http://www.adobe.com/2006/flex/mx/internal::doUpdateDisplayList()[E:\dev\4.x\frameworks\projects\spark\src\spark\primitives\supportClasses\GraphicElement.as:3893]
      at spark.primitives.supportClasses::GraphicElement/validateDisplayList()[E:\dev\4.x\frameworks\projects\spark\src\spark\primitives\supportClasses\GraphicElement.as:3877]
      at spark.components::Group/updateDisplayList()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\Group.as:956]
      at spark.skins::SparkSkin/updateDisplayList()[E:\dev\4.x\frameworks\projects\spark\src\spark\skins\SparkSkin.as:191]
      at mx.skins.spark::SparkSkinForHalo/updateDisplayList()[E:\dev\4.x\frameworks\projects\sparkskins\src\mx\skins\spark\SparkSkinForHalo.as:118]
      at mx.skins.spark::ButtonSkin/updateDisplayList()[E:\dev\4.x\frameworks\projects\sparkskins\src\mx\skins\spark\ButtonSkin.mxml:66]
      at mx.core::UIComponent/validateDisplayList()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:8709]
      at mx.managers::LayoutManager/validateClient()[E:\dev\4.x\frameworks\projects\framework\src\mx\managers\LayoutManager.as:932]
      at mx.core::UIComponent/validateNow()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:7792]
      at mx.controls::Button/http://www.adobe.com/2006/flex/mx/internal::viewSkinForPhase()[E:\dev\4.x\frameworks\projects\framework\src\mx\controls\Button.as:1985]
      at mx.controls::Button/http://www.adobe.com/2006/flex/mx/internal::viewSkin()[E:\dev\4.x\frameworks\projects\framework\src\mx\controls\Button.as:1885]
      at mx.controls::Button/commitProperties()[E:\dev\4.x\frameworks\projects\framework\src\mx\controls\Button.as:1631]
      at mx.core::UIComponent/validateProperties()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:7933]
      at mx.managers::LayoutManager/validateProperties()[E:\dev\4.x\frameworks\projects\framework\src\mx\managers\LayoutManager.as:572]
      at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.x\frameworks\projects\framework\src\mx\managers\LayoutManager.as:700]
      at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.x\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1072]
  </code></pre>