Wednesday, June 29, 2005

Read only properties in AS2

Since there is no final keyword in AS2 making public class properties read only is a little tricky.
class Color{
public static var BLACK:Number=0x000000;
}

but this is unsafe as the user can change this property.

Color.BLACK=0xffffff;

Therefore to make it read only we can simply declare it as a get method and no set method.

class Color{
public static function get BLACK():Number{
return 0x000000;
}
}

Now if the user tries to set this property

Color.BLACK=0xffffff;

the compiler will generate an error.

firdosh

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home