6 Replies Latest reply on Apr 26, 2007 8:02 AM by Blue_Pixel

    importing classes into Flex apps

    JediJon
      I am an experienced AS 2.0 writer, but I am new to Flex. In fact, I am in the process of taking the AS 3.0 training through Lynda.com with Joey Lott, a superb teacher in my opinion. However, the thing I am finding most confusing is knowing when to import a class and what to import. With 2.0, most of the classes are extending the MovieClip class, which is automatically imported into every Flash file. Now, it seems I have to import a new class just to accomplish the most simple tasks, like clicking a button, creating a button, or even going to a URL. I find it very frustrating that I cannot infer what classes to import, that I have to wait for an error which isn't intuitively written itself, much less WHEN to import anything. Does anyone have a suggested reading or tutorial that could help me get a grip on this seemingly declined feature of the language's evolution? Better yet, since I have not yet seen Flash 9, does Flash 9 continue to do much of its own class importation, and in the meantime am I just dealing with the hard knocks of Flex? Many thanks to anyone who's been in this boat and can lend me some encouragement.
        • 1. Re: importing classes into Flex apps
          peterent Level 2
          You will have to import a lot more classes in ActionScript 3. That's because the language has become more mature and having complete definitions makes the compiler work faster and generate better, more efficient code.

          If you use Flex Builder 2, you'll get some help. For example, suppose you want to create a DataGridColumn in ActionScript, but you don't know what class to import. If you begin to type "var col:" when you hit the colon, FB will display a list of possible data types. If you scroll down that list (or start typing DataGridColumn) and select DataGridColumn, FB will add the import for you:

          import mx.controls.dataGridClasses.DataGridColumn;

          You can also use a wildcard import such as flash.events.* where the * means to include all of the classes in that package. But don't worry, the compiler will only put into the SWF the classes actually used. I find it better to be explicit about the classes because it tells the reader exactly which foreign classes are being used and where they come from.

          I use this feature of Flex Builder all the time; I can't remember where all of the classes live.

          Hope this gives you more confidence.
          • 2. Re: importing classes into Flex apps
            Rahul Mainkar Level 1
            I surely agree with Peter. Even I was confused initially as there are so many classes in AS 3.0 and its practically impossible to remember all of the. The obvious choice is Flex Builder 2.0.1. It provides a great programming environment.

            Rahul
            • 3. Re: importing classes into Flex apps
              Blue_Pixel
              What about the case when I want to import my own class? I can't write " import myScript/myClass.as" or if I write include "myScript/myClass.as" the I recieve an error : "Classes must not be nested.".
              My class is something like:
              public class myclass
              {
              public var id:int;
              public var cod:String;
              public var surname:String;
              }

              • 4. Re: importing classes into Flex apps
                peterent Level 2
                Your class is in the file: myScript/myClass.as which means it must look like this:

                package myScript {
                public class myClass {
                ...
                } // end of class
                } // end of package

                Notice that the package name is the folder name. If your file were myScript/myComponents/myClass.as then the package name would be myScript.myComponents - the package name reflects the folder structure, except it uses dots and not slashes.

                Also: the class and package names are case sensitive. While Windows doesn't care if your file is myclass.as or myClass.as, ActionScript does care.

                To use your class you import it:

                import myScript.myClass;
                or
                import myScript.myComponents.myClass;

                Again, dots not slashes.

                If your class is in your source-path for Flex Builder (and it almost certainly is unless you've done something extraordinary), Flex Builder will find your class to:

                var test:my (now Flex Builder will display myClass) if you pick it, FB will automatically add the import.

                The thing to note here is that your classes aren't any different from the Flex framework classes.
                • 5. Re: importing classes into Flex apps
                  JediJon Level 1
                  Thanks Peter and Rahul ! That really, really helps. I didn't realize FB would auto insert the class upon variable declaration. It's probably the case that the tutorials I am studying are trying to emphasize the fact that class importation is such a big part of development now, therefore the tuts aren't quite yet revealing the auto-import aspect. I'll do my best to keep only the classes I need, as you do with your work, in order to keep the compilation as fast as possible. You are right, the language has matured. It is a true object-oriented language now, and what I am experiencing is just growing pains. I will plug on through. Your wisdoms are much appreciated, and again, Thank You!
                  :)
                  • 6. Re: importing classes into Flex apps
                    Blue_Pixel Level 1
                    Thank you Peter, it does work now.