2
votes

Question: I get an annoying error: I try to use the type Size in the below actionscript code. But I always get this error:
1046: Type was not found or was not a compile-time constant: Size.
But no matter what I try, it seems to break when I add
public function get Size():Size
Somehow it doesn't like the return type Size, but what am I doing wrong ?

package BaseTypes
{
    public class StockData 
    {
        private var size:Size;
        public function get Size():Size
        {
            return this.size;
        }
        public function set Size(value:Size):void
        {
            this.size = value;
        }
    } // End Class
}// End Package

I have the bellow code in folder BaseTypes, and it is in the AS3 class path.

package BaseTypes
{
    public class Size  
    {
        private var width:Number;
        private var height:Number;
        public function Size(width:Number, height:Number)
        {
            this.width = width;
            this.height = height;
        }
        public function get Width():Number
        {
            return this.width;
        }
        public function set Width(value:Number):void
        {
            this.width = value;
        }
        public function get Height():Number
        {
            return this.height;
        }
        public function set Height(value:Number):void
        {
            this.height = value;
        }
        public function toString():String
        {
            return "{width: " +this.width.toString()+ " height: " +this.height.toString() + "}";
        }
    } // End Class
} // End package
3

3 Answers

5
votes

You have named your getter function the same as the class you imported, I think this is the source of your error.

in your StockData class you have the definition

public function get Size():Size

you should rename "Size" to either "size" or something that is not the same name as the class you imported.

2
votes

Les pointed out the problem. The following will work:

class StockData 
{
    private var _size:Size;
    public function get size():Size { return _size; }

    public function set size(value:Size):void {
        _size = value;
    }
}

unlike in C# where PascalCase is used for everything, by convention in AS3, people use UpperCamelCase for classes and interfaces and lowerCamelCase for about everything else (ALL_CAPS for global constants and under_scores for package names). as you can see, the AS3 compiler cannot resolve the duplicate meaning of Size. This is obviously a compiler flaw, but I wouldn't wait until Adobe fixes it. However using said convention, you can eliminate such ambiguities. also: private/internal/protected vars are generally prefixed by _ or $ to avoid collision with accessors.

If you're using FlashDevelop, I suggest you use Ctrl+M to jump to matching braces (no need to mark // End Class). If you're not, I heavily suggest, you give it a try/

0
votes

Add following to your StockData imports as Size belongs to different package.
import BaseTypes.Size;
If you are using Flash IDE, then add the directory containing your BaseType package to ClassPath.