Search code examples
actionscript-3oopclassflex4package

Flex 4 two classes in one file


Can I put two or more actionscript classes in one .as file like this:

//A.as
package classes {

    public class A {
        public function A() {
            var b:B = new B();
        }
    }
    internal class B {
        public function B() {
            trace("Hello");
        }
    }
}

It doesn't work in Flash Builder:

A file found in a source-path can not have more than one externally visible definition. classes:A; classes:B

If it possible, I'm going to ask next question.
Can I place two or more packages with multiple classes in one .as file?


Solution

  • No and no. The following works:

    //A.as
    
    package classes {
    
        public class A {
            public function A() {
                var b:B = new B();
            }
        }
    
    }
    class B { // <--- Note the class is outside of the package definition.
        public function B() {
            trace("Hello");
        }
    }
    

    The class B is only visible to the class A - you cannot have more than one visible class in one file (exactly what the error message states). And you cannot have more than one package in a file.