Actionscript: undefined public variables?

I have a class like this ..

public class Doc {
  public function Doc():void {}

  public var myVar:Boolean;
}

      

How do I know if the value stored by myVar has a default value of false, or if someone assigned it to false?!? Isn't there undefined state? How can I achieve this?

+2


source to share


2 answers


Make the property myVar a and use another variable to check if it's not explicitly set.



public class Doc 
{
  public function Doc():void {}

  private var _myVar:Boolean;
  private var myVarSetExplicitly:Boolean = false;
  public function get myVar():Boolean
  {
    return _myVar;
  }
  public function set myVar(value:Boolean):void
  {
    myVarSetExplicitly = true;
    _myVar = value;
  }
}

      

+5


source


You cannot use boolean, it defaults to false and false === false.

You could not strictly inject a variable and then use a getter and setter for type protection

public class Doc {
  private var _myVar;

  public function set myVar(value:Boolean){
    _myVar = value;
  }

  public function get myVar(){
    return _myVar;
  }
}

      



Now that its not set myVar should be === null and you can only then set it to boolean.

But it feels a little hacky and I'm wondering why you need to tell the difference.

0


source







All Articles