7 Replies Latest reply on Mar 22, 2010 6:28 PM by alice_data

    Constructor functions must be instance methods?

    alice_data Level 1

      Hi,

       

      I have this code as in the following:

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
          <mx:Script>
              <![CDATA[
                  import mx.controls.*;           
                  import mx.events.*;
                  import mx.containers.*;       
                  import thematicMap.*;  
                   
                   public var Region:Region = new Region();
                   public var Scale:Scale = new Scale();
                   public var ThematicMap:ThematicMap = new ThematicMap(); //1026:Constructor functions must be instance methods
                   public var ReadXML:ReadXML = new ReadXML();       
                   private var savedIndex:int= 99999;        
                                                        
                    public function addNavigation():void{
                       
                        var buttonTools:Array = ["Driving Distance","Education Level--College",
                                                 "Education Level--High School","Median Income","Population"];
                        var navigationBar:ToggleButtonBar = new ToggleButtonBar();
                            navigationBar.dataProvider = buttonTools;
                            navigationBar.toggleOnClick = true;
                            navigationBar.selectedIndex = -1;
                            navigationBar.addEventListener(ItemClickEvent.ITEM_CLICK,clickHandler);                      
                            //pass the variables?
                            addSlider(); //add the slider control  
                            vbox.addChild(navigationBar);
                    }
                    public function addSlider():void{
                       
                         var text_entry:HBox = new HBox();                 
                         var numColors:TextInput = new TextInput();
                             numColors.length == 3;
                             numColors.text = "5";
                             numColors.restrict = "0-9";
                         var direction:Text= new Text();
                             direction.text = "Number of Colors";
                          //recalculate the map if the listener is called
                        
                         text_entry.addChild(direction);
                         text_entry.addChild(numColors);
                         vbox.addChild(text_entry);                    
                    }            
                
                   public function clickHandler(event:ItemClickEvent):void{
                       if(event.index == savedIndex) {
                           //don't do a thing
                       }
                       else savedIndex = event.index;           
                       //add the thematic map                
                       ThematicMap.addRegions(OK); //This part never worked
                       //mapGrid.addChild(thematicmap);
                   }
              ]]>
          </mx:Script>
         <mx:Canvas id="map" creationComplete="addNavigation()">
                 <mx:VBox id="vbox" x="20" y="10">           
                 <mx:Canvas id="mapGrid"/>      
          </mx:VBox>     
         </mx:Canvas>
      </mx:Application>

       

      Here is what I have in my ThematicMap.as:

       

      package thematicMap
      {
          import mx.core.*;
          import mx.events.*;
         
          public class ThematicMap {       
             
              public var region:Region = new Region();  
              public var readXML:ReadXML = new ReadXML();     
             
              public function addRegions(name:String):void {   
                   trace(name);      
              }
          }
      }

       

      Have I done something wrong here? How come it keeps telling me that the constructor functions must be instance methods?

      Thanks for your help.

       

      Alice

        • 1. Re: Constructor functions must be instance methods?
          jphillipcamp

          Add a constructor function as seen below:

           

          package thematicMap
          {
              import mx.core.*;
              import mx.events.*;
             
              public class ThematicMap {       
                 
                  public var region:Region = new Region();  
                  public var readXML:ReadXML = new ReadXML(); 

           

                  public function ThematicMap() {}
                 
                  public function addRegions(name:String):void {   
                       trace(name);      
                  }
              }
          }

           

          I hope this helps!

           

          Best,

           

          J. Phillip Camp

          • 2. Re: Constructor functions must be instance methods?
            alice_data Level 1

            Hi,

             

              I took your advice, and looks like it does not seem to work. Instead, I changed all my variable instances of these "classes" to lowercase, and that helped. For some reason, the error I get now refers to the function I tried to get to work earlier now shows up by teling me I am caling an possibly undefined method addRegions through a reference with static type ThematicMap, and there is an undefined property OK.

             

            Here is the code in my ThematicMap class:

             

             

              package thematicMap
            {
                import mx.core.*;
                import mx.events.*;
               
                public class ThematicMap {       
                   
                    public var region:Region = new Region();  
                    public var readXML:ReadXML = new ReadXML(); 
                          
                    public function addRegions(name:String):void {   
                         trace(name);                         

                    }
                }
            }

             

            And, in my app:

             

            public function clickHandler(event:ItemClickEvent):void{
                             if(event.index == savedIndex) {
                                 //don't do a thing
                             }
                             else savedIndex = event.index;           
                             //add the thematic map                
                             thematicMaps.addRegions(OK);
                             //mapGrid.addChild(thematicmap);
                         }

             

            Anyone has any ideas on what this might be?

            Thanks for your help.

             

            Alice

            • 3. Re: Constructor functions must be instance methods?
              jphillipcamp Level 1

              All variables should always start lowercase.  Otherwise the compiler will think they are classes.  Also but quotes around ok and you should be good to go!

               

                thematicMaps.addRegions("OK");

              • 4. Re: Constructor functions must be instance methods?
                alice_data Level 1

                Yes, I put in the quotes, and that took care of that error.

                However, it still tells me I have an error that I am calling a possibly undefined method addRegions through a reference with static type ThematicMap

                 

                Here is the complete code of my main app:


                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
                    <mx:Script>
                        <![CDATA[
                            import mx.controls.*;           
                            import mx.events.*;
                            import mx.containers.*;       
                            import thematicMap.*;   //I had imported all the classes
                             
                             public var region:Region = new Region();
                             public var scale:Scale = new Scale();
                             public var thematicMaps:ThematicMap = new ThematicMap();
                             public var readXML:ReadXML = new ReadXML();       
                             private var savedIndex:int= 99999;        
                                                                  
                              public function addNavigation():void{
                                 
                                  var buttonTools:Array = ["Driving Distance","Education Level--College",  "Education Level--High School","Median Income","Population"];
                                  var navigationBar:ToggleButtonBar = new ToggleButtonBar();
                                      navigationBar.dataProvider = buttonTools;
                                      navigationBar.toggleOnClick = true;
                                      navigationBar.selectedIndex = -1;
                                      navigationBar.addEventListener(ItemClickEvent.ITEM_CLICK,clickHandler);                      
                                      //pass the variables?
                                      addSlider(); //add the slider control  
                                      vbox.addChild(navigationBar);
                              }
                              public function addSlider():void{
                                 
                                   var text_entry:HBox = new HBox();                 
                                   var numColors:TextInput = new TextInput();
                                       numColors.length == 3;
                                       numColors.text = "5";
                                       numColors.restrict = "0-9";
                                   var direction:Text= new Text();
                                       direction.text = "Number of Colors";
                                    //recalculate the map if the listener is called
                                  
                                   text_entry.addChild(direction);
                                   text_entry.addChild(numColors);
                                   vbox.addChild(text_entry);                    
                              }            
                          
                             public function clickHandler(event:ItemClickEvent):void{
                                 if(event.index == savedIndex) {
                                     //don't do a thing
                                 }
                                 else savedIndex = event.index;           
                                 //add the thematic map                
                                 thematicMaps.addRegions("OK");
                                 //mapGrid.addChild(thematicmap);
                             }
                        ]]>
                    </mx:Script>
                   <mx:Canvas id="map" creationComplete="addNavigation()">
                           <mx:VBox id="vbox" x="20" y="10">           
                           <mx:Canvas id="mapGrid"/>      
                    </mx:VBox>     
                   </mx:Canvas>
                </mx:Application>

                 

                As a matter of fact, when I write import thematicMap. it looks like thematicMap.ThematicMap does show up, so that is imported, right?

                I am getting confused.

                 

                Alice

                • 5. Re: Constructor functions must be instance methods?
                  jphillipcamp Level 1

                  I copied your code and got it to compile with a few hitches:

                  • I had to make sure ThematicMap.as was in the thematicMap directory in the source so the package name was correct.
                  • I had to check all the spelling of the file names and directories (I have typo issues - I had typed ThermaticMap, and I was getting a similar issue to yours)
                  • I had to comment out Region, Scale and ReadXML

                   

                  Other than that, it worked.  I'd check the file name, directory structure, and package name and go from there.

                  • 6. Re: Constructor functions must be instance methods?
                    Flex harUI Adobe Employee

                    Clean and rebuild then show us exactly the error and what line.

                    • 7. Re: Constructor functions must be instance methods?
                      alice_data Level 1

                      My code is now working great thanks to the users who replied to the posts. I changed the name of the class, and that solved the other part of the problem.

                       

                      Thanks.

                       

                      Alice