Importing Classes in Flex and Flash
DIGG IT!
3
Comments
Published
Saturday, July 09, 2005
at
8:55 AM
.
I have had trouble with 'import' with Flash and Flex in larger projects. I have witnessed time and time again small quirks in 'import' that can wreck a project in later development.&
'import' is very useful and in 95% of cases works as intended. The problem is that import changes the compiler scope when classes are imported. If you are not careful about the end class name, your class names can collide with local variables, global classes and other classes. When you are using multiple packages, you must make sure the combined classes do not have naming issues less you instantiate the wrong class or overwrite something unintentional.
So is there another option? YES! There is a very simple way to get classes, subclasses and super classes into your application. The Flash and Flex compiler looks for class paths during compilation. If there is a match to a class, the class will be added into the application automatically.
Example Code
Say I have 3 classes in a package called 'com.dog'. Listing the classes I wanted to import works like so:
com.dog.Frog;
com.dog.Cog;
com.dog.Log;
trace(com.dog.Log.name); //Log
trace(com.dog.Cog.name); //Cog
trace(com.dog.Frog.name); //Frog
//these classes are not used in the compiler scope so they fail as expected.
trace(Log.name); //undefined
trace(Cog.name); //undefined
trace(Frog.name); //undefined
Using import I could do this:
import com.dog.*
trace(com.dog.Log.name); //Log
trace(com.dog.Cog.name); //Cog
trace(com.dog.Frog.name); //Frog
trace(Log.name); // Log
trace(Cog.name); // Cog
trace(Frog.name); // Frog
The real difference is scope pollution. If you use too many imports, you are almost assured to run into problems if you are not careful with class naming.
Another issue here is that Flash MX 2004 has a compiler bug that prevents the automatic import of super classes on occasion. The bug can be easily averted in listing the super class without the import statement.
Just to be clear, I use 'import' in my projects. I understand what it does and know the quirks. That said, simply listing the path to your class is valid and works well too.
Cheers,
ted ;)

So the real "problem" seems not to be import per se, but wildcards in imports. If you don't use wildcards, any possible clash of classes becomes obvious.
As for the superclass problem: I've never come across this, can you elaborate?
Cheers,
Ralf.
The compiler bug has to do with no importing a superClass unless it was mentioned explicitly outside of the class.
Even with an straight import the compiler cope is modified. Say I did this:
import com.donut.Array
import com.donut.Bonus
a = new Array()
b = new Bonus()
The Array will be the _global.Array not com.donut.Array where Bonus will be com.donut.Bonus. Downstream you will find you were using Array vs using an com.donut.Array. Where there are many classes to import this gets even worse unless your class names are unique.
Cheers,
Ted ;)
I just tried something along your example and it works as expected (in flash). Is this a flex problem you are talking about?
Ralf