5 Replies Latest reply on Oct 8, 2009 2:49 PM by paul.williams

    packages and 'packages cannot be nested', Flex Builder

    FlexNewbie999

      I'm going through the LiveDocs tutorial at http://livedocs.adobe.com/flex/ and I'm having problems defining packages and referencing them in .mxml files.

      For example:

      I have an .as class in the includes package in the src/includes folder defined as:

      package includes
      {
          public class Sample3Script
          {
              public function Sample3Script()
              {

              }

          }

      }

       

      I have an include statement in a .mxml file in the src directory defined as:

      <mx:Script source="includes/Sample3Script.as"/>

      When I run the mxml file I get a 'Packages cannot be nested' error in the .as file. What is the CORRECT way
      define packages in Flex Builder and create classes inside of them? I'm a Java developer used to Eclipse so
      I'm sure I'm missing something here.

      Thanks in advance!
        • 1. Re: packages and 'packages cannot be nested', Flex Builder
          paul.williams Level 4

          You should be using "import" rather than "include". The link below may be helpful (although it isn't a great explanation).

           

          http://livedocs.adobe.com/flex/3/html/help.html?content=usingas_4.html

           

          The import statement in AS works in the same way as in Java whereas the include statement has the same effect as copy and pasting the source of one file into another. Since Sample3Script is a class, you can only import it. If you include one class in another class, as you are below, you will end up with nested package statements.

           

          You import a class within a script block as follows:

           

          <mx:Script>
               <![CDATA[
                   import includes.Sample3Script;
               ]]>
          </mx:Script>


          • 2. Re: packages and 'packages cannot be nested', Flex Builder
            FlexNewbie999 Level 1

            Thanks for the tip! I've cleared up the nested packages issue and now I have 'call to a possibly undefined method calculate'. 'calculate()'  is a public method in a package that is imported properly via :

             

              <mx:Script>
                  <![CDATA[
                      import myControls.MyButton;
                  ]]>
              </mx:Script>

             

            What is a 'possibly undefined method' ?

             

            Thanks!

            • 3. Re: packages and 'packages cannot be nested', Flex Builder
              paul.williams Level 4

              Can you post the code where you are calling the calculate() method (ie. the line that is showing the error)?

              • 4. Re: packages and 'packages cannot be nested', Flex Builder
                FlexNewbie999 Level 1

                Thanks for the quick reply. The code for the method and its class follows:

                package myControls
                {
                    import mx.controls.Button;
                   
                    public class MyButton extends Button
                    {
                        public function MyButton()
                        {
                        }

                 

                    }
                   
                    // usingas/includes/Sample3Script.as
                public function calculate(s:String):String {
                    var n:Number = Number(s);
                    var t:Number = Math.round((n-32)/1.8*10)/10;
                    return String(t);
                }


                }

                 

                The code in mxml file that references the method follows:

                <mx:Script>
                      <![CDATA[
                          import myControls.MyButton;
                      ]]>
                  </mx:Script>
                  <mx:Panel title="My Application" paddingTop="10" paddingBottom="10"
                     paddingLeft="10" paddingRight="10">
                     <mx:HBox>
                        <mx:Label text="Temperature in Fahrenheit:"/>
                        <mx:TextInput id="fahrenheit" width="120"/>

                // THIS IS THE OFFENDING LINE - error is '1180 - call to possibly undefined method calculate()'
                        <mx:Button label="Convert" click="celsius.text=calculate(fahrenheit.text);"/>

                 


                        <mx:Label text="Temperature in Celsius:"/>
                        <mx:Label id="celsius" width="120" fontSize="24"/>
                     </mx:HBox>
                  </mx:Panel>

                 

                Thanks again!

                • 5. Re: packages and 'packages cannot be nested', Flex Builder
                  paul.williams Level 4

                  Your MyButton class has a public method called 'calculate', so you can only call this method on an object that is an instance of MyButton. You aren't creating any instances of MyButton in your MXML class, so you won't be able to access the calculate method in its current state. Java works in a similar same way.

                   

                  One solution would be to replace your standard Flex button with an instance of MyButton eg:

                   

                  <myControls:MyButton

                       id="myButton"

                       label="Convert"

                       click="celsius.text=myButton.calculate(fahrenheit.text);"/>

                   

                  In order to use MyButton in your MXML file you will also need to add an additional namespace at the top of your file. This is the MXML equivalent of an import in ActionScript - you should see one for "mx" there already. The new namespace will look like this:

                   

                  xmlns:myControls="myControls.*"

                   

                  This tells the MXML compiler where to look for the definition of MyButton.

                   

                  Also, since you are instantiating your MyButton class in MXML, you don't need the import statement for MyButton, only the namespace described above. There's a bit more info on this here:

                   

                  http://www.flexafterdark.com/docs/MXML-Namespaces