2 Replies Latest reply on Jun 8, 2006 10:02 AM by Peter Lorent

    Attribute used outside class

    ChrisFlynn Level 1
      So I have a class. Within this class. I have a function that I want to declare local variables for.

      So it looks something like this:

      class MyClass{
      //DECLARATIONS OF MyClass
      public var MyClassArr:Array = [];
      //CONSTRUCTOR FUNCTION
      public function MyClass(c:Object) {
      constructThis(c);
      }
      public function constructThis(c:Card):Void {
      MyClassArr[MyClassArr.length] = c;
      }
      public function someFunction(MyClassArr:Object) {
      var arr1:Array;
      var arr2:Array;
      var num:Number;
      //...
      //Other routines
      //...
      }
      }

      My problem is with these 3 variable I declare within someFunction.

      var arr1:Array;
      var arr2:Array;
      var num:Number;

      My original thought was that these 3 would be local variables, and since outside this class I don't care about them I tried to declare them like this:

      So it looks something like this:

      class MyClass{
      //DECLARATIONS OF MyClass
      public var MyClassArr:Array = [];
      //CONSTRUCTOR FUNCTION
      public function MyClass(c:Object) {
      constructThis(c);
      }
      public function constructThis(c:Card):Void {
      MyClassArr[MyClassArr.length] = c;
      }
      public function someFunction(MyClassArr:Object) {
      var arr1:Array;
      var arr2:Array;
      var num:Number;
      //...
      //Other routines
      //...
      }
      }

      My problem is with these 3 variable I declare within someFunction.

      private var arr1:Array;
      private var arr2:Array;
      private var num:Number;

      but that's when i get this error message, "Attribute used outside class". So then I declared them as public.

      public var arr1:Array;
      public var arr2:Array;
      public var num:Number;

      Same error. Its only when I declare them without the "public" or "private" that I can get this to work, which seems to go against everything Ive read about AS 2.0 so far (and yes I am an AS 2 newb).

      If anyone can point me in the right direction, that would be great.

      Thanks
      Chris
        • 1. Re: Attribute used outside class
          blemmo Level 1
          Hi,

          'private' and 'public' are keywords for class variables or functions which are available throughout the whole class. Local variables only exist in the function where they are declared, and they are destroyed when the function finishs. So they don't need a 'private' or 'public' statement, because they cannot be accessed from outside the function anyways.
          The compiler 'thinks' they should be class variables if they have such a statement, that's why it generates the error, to let you know that they are declared in the wrong place.
          In short: don't use these statements for local vars, because they are kind of private by nature.

          hth,
          blemmo
          • 2. Re: Attribute used outside class
            Peter Lorent Level 2
            When writing a class it's best to keep in mind you are creating an object. That object has properties which can be available only to the object or to other objects too. At the top of your class you declare these properties in what's known as the class definition. So when you type:
            public var MyClassArr:Array;
            you give your object a property MyClassArr which is an array and is available to all other objects in your project since it is public.

            Then the next part of the class is the class body which defines the methods of the object. These too can be private or public (and static). Within the methods of the objects you can access the properties of the object (in your example - access the MyClassArr). Or, use local variables (local to the function) which you declare with the keyword var. Within the methods you can't add new properties to the class, meaning you simply can't use the public or private keyword there.