This content has been marked as final. Show 2 replies
'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.
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.