1
votes

If I include a an external actionscript file in a flex mxml file, I get different behaviours at compile time depending on the method used. Using an mx script tag with a source attribute or an include statement, then compiling the file gives errors like:

Error: Packages cannot be nested.

If use import these errors go away and the file compiles but then I have problems when trying to instantiate the class.

    import lib.journal;
    public var testing:journal = new journal();
    testing.init();

which gives:

Error: Access of undefined property testing.

Can anyone explain what is going on here? What effect does including the file as opposed to importing it have on packages and scope?

thanks,

3

3 Answers

4
votes

To answer the more general question: importing is the preferred way of including external files. In my opinion the include statement should be only used when nothing else will do as it makes things a bit more difficult to debug if something goes wrong and makes code usually more difficult to read and comprehend. Assaf's description of what import and include do is correct.

And then for the more specific problem you seem to have: you're probably trying to do the testing.init(); right there in the <script> block -- try putting it in a method. You should only have things like import statements and member declarations (variables, functions) directly in the script block and statements like this within functions.

You're seeing that error message because when you're calling the init() method of this object, it hasn't been created yet -- that statement will be executed when the definition of the class that your MXML file represents is loaded; what you want is to have it executed when a particular instance of this class has been created, and you can do that by calling it in the constructor of the class (this is, as far as I know, not possible when you're writing a class using MXML, so read on:) or for example in a handler function for the FlexEvent.CREATION_COMPLETE (or creationComplete in terms of MXML tag attributes) event (see the example below.)

Try something like this:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml"
    width="800" height="600"
    creationComplete="creationCompleteHandler();"
>
    <mx:Script>
        <![CDATA[

        import lib.Journal;

        public var testing:Journal = new Journal();

        private function creationCompleteHandler():void
        {
            testing.init();
        }

        ]]>
    </mx:Script>

</mx:Application>
2
votes

Import lets you access the public declarations of the imported file.

Include plants the contents of an as3 file into the include location as if you typed it there.

See the manual on "Comparing, including, and importing ActionScript code".

0
votes

Try this:

<mx:Script>
    <![CDATA[

    import lib.Journal;

    public var testing:Journal;

    private function creationCompleteHandler():void
    {
        testing = new Journal();
    }

    ]]>
</mx:Script>

Cheers, Dirk