ActionScript 3: Imports vs Includes

There are two different ways that you can use ActionScript 3 code inside a Flex Component or ActionScript class. One way is to use an import statement, the other way is to use an include. This topic came up recently on the FlexCoder's list where one person was trying to use include when he should have used an import. I thought I'd talk a bit about the difference.

The import directive is the one I I use most often. It allow you to make use of one ActionScript class from within another.

Let's pretend we have this simple ActionScript class:


package comp
{
    public class TestClass
    {
        public static function getFoo():String{
            return "Foo";
        }
    }
}

I gave this class a single method named getFoo. It returns a string. I also made the method a static method, so for the purposes of this example we don't have to worry about creating an instance of a class.

We can import the class into the main application, like this


<mx:Script>
    <![CDATA[
        import comp.TestClass;
        import mx.controls.Alert;
    ]]>

</mx:Script>
<mx:Button click="Alert.show(TestClass.getFoo())" label="Get Foo" />

This code imports two classes; our custom one and the Alert class. The button will call the method and show the value. That is the simple example of an import.

When writing ActionScript, you'll find yourself using the import all the time, often importing the framework classes as well as your own to put together your application, just like the example above. Includes. Your include file should only contain a code snippet, not a class definition. Repeating our previous example. This is the include:


public static function getBar():String{
    return "Bar";
}

And it is included like this:


<mx:Script>
    <![CDATA[
        include "comp/bar.as";
    ]]>

</mx:Script>
<mx:Button click="Alert.show(getBar())" label="Get Bar" />

In this case, the include file is added as part of our main Application. In the first case, they are kept separate. Both offer valid reuse cases.

In the Flex Framework, an include is used to define the version numbers of the class. In my own development I once used an included to populate a state drop down. Since states don't change often, I Decided not to populate the drop down dynamically. I used a hard coded dataProvider. The 50+ lines of code needed to create the dataProvider was getting tedious to scroll back and forth through when editing the component. Removing that code from the main component and putting it in an include allowed to simplify those 50 lines of code into 1. The compiler places the include code back into the main component before turning it into binary goodness.

So, in summary, Imports are always performed on classes. Includes are always performed on independent code snippets. Each has their place in developing ActionScript code.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Josh's Gravatar You can bring your include import down to one line:

<mx:Script source="comp/bar.as"/>
# Posted By Josh | 10/15/07 7:04 PM
Jeffry Houser's Gravatar Sweet, just like JavaScript.

Thanks for letting me knw
# Posted By Jeffry Houser | 10/15/07 7:56 PM
Hal's Gravatar How do you have actionscript only import a file one time, like php's include_once directive?
# Posted By Hal | 1/22/08 3:00 AM
Jeffry Houser's Gravatar Hal,

I don't know PHP very well, so I'm not sure exactly what you're asking for. Can you elaborate? I believe the compiler will remove unused imports from the resulting swf file.
# Posted By Jeffry Houser | 1/22/08 10:24 AM
Anonym's Gravatar Maybe you should explain why i need to type public STATIC var test:String; in a include scenario?

Looking around for hours and not finding much explanaton as to why i need to use STATIC. Is it because the included file is only loaded once!?
# Posted By Anonym | 4/3/09 10:07 AM
Jeffry Houser's Gravatar Hi Anon,

I blogged about your question

http://www.jeffryhouser.com/index.cfm/2009/4/9/Why...

The short answer is that there is that you are not limited to static variables in an include file. I suspect your problems stem from something different.
# Posted By Jeffry Houser | 4/8/09 5:45 PM
Andre's Gravatar Hi, i've got a problem:

i have a pice of code that serves as a base template that i'm including in the main application likes this:

      <mx:Script source="mxmlBase.as"></mx:Script>
      <mx:Script source="Main.as"></mx:Script>   

that code in mxmlBase i want to reference in Main.as.
but here's the prob.
when i compile everything works, but: i'm using Flasdevelop and inside main.as i don't get any code completion or code help when i want to access variables that are declared in mxmlBase.

am i doing something wrong, or is this not possible?
thanks
# Posted By Andre | 4/20/09 7:54 PM
Andre's Gravatar Hi, i've got a problem:

i have a pice of code that serves as a base template that i'm including in the main application likes this:

      <mx:Script source="mxmlBase.as"></mx:Script>
      <mx:Script source="Main.as"></mx:Script>   

that code in mxmlBase i want to reference in Main.as.
but here's the prob.
when i compile everything works, but: i'm using Flasdevelop and inside main.as i don't get any code completion or code help when i want to access variables that are declared in mxmlBase.

am i doing something wrong, or is this not possible?
thanks
# Posted By Andre | 4/20/09 7:55 PM
Jeffry Houser's Gravatar Andre,

I never used FlashDevelop, but if I had to theorize... how would the Main.as file know about anything that happens inside mxmlBase?

Include files are, generally, separate entities. How can two independent entities have knowledge of each other?

To me this seems like expected behavior.
# Posted By Jeffry Houser | 4/20/09 8:37 PM
All Content Copyright 2005, 2006, 2007, 2008, 2009 Jeffry Houser. May not be reused without permission
BlogCFC was created by Raymond Camden. This blog is running version 5.9.2.002.