Workaround for namespace bug with getter/setter in Flex 2

ActionScript, Flex Add comments

Consider the following code:

Actionscript:
  1. public class MyClass {
  2.   private var _myProperty:String;
  3.  
  4.   public function MyClass() {
  5.     this.myProperty = myProperty;
  6.   }
  7.  
  8.   protected function get myProperty():String {
  9.     return _myProperty;
  10.   }
  11.  
  12.   private function set myProperty(value:String):void {
  13.     _myProperty = value;
  14.   }
  15. }

You wouldn't expect this to result in an error, though it gives you the error "1000: Ambiguous reference to myProperty". Apparently the Flex compiler can't handle the fact that you create a getter and a setter for a property with a each a different namespace. This is filed as an issue with the Flex 2 compiler.

174646: If a class contains accessor functions with different access control namespace attributes, (for example, aprotected setter and a public getter) using one of them causes a compile-time-error, for example,Compiler-Error 1000: Ambiguous reference to myVar
The workaround is to rename your getter or setter function to avoid the mismatch.

Since I don't like to rename my getter or setter, here's another solution:

Actionscript:
  1. public class MyClass {
  2.   private var _myProperty:String;
  3.  
  4.   public function MyClass() {
  5.     private::myProperty = myProperty; // !!!
  6.   }
  7.  
  8.   protected function get myProperty():String {
  9.     return _myProperty;
  10.   }
  11.  
  12.   private function set myProperty(value:String):void {
  13.     _myProperty = value;
  14.   }
  15. }

Simply replace the call to this.myProperty with private::myProperty (or the access control namespace you're using) to work around this.

I don't know if this will be fixed in Flex 3. Can anybody confirm?


Add to Bloglines - Digg This! - del.icio.us - Stumble It! - Twit This! - Technorati links - Share on Facebook - Feedburner
 

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in