views:

35

answers:

2

I have several data manipulation widgets that all implement a custom IPropertyEditor interface. I would like to include a GetValue getter, but the return types could be String, Boolean or int, off the top of my head. In AS3, all of that inherits from Object, so I could have GetValue return an object, but I don't feel great about that approach. At the risk of asking a subjective question, does anyone have any recommendations on how to approach this?

+1  A: 

In ActionScript I'm pretty sure you can set a variable return type by defining a function in the following way:

public function getValue():* {
    return "Any of these would be fine.";
    return true;
    return 1;
    return new Sprite();
}

Hope that helps.

icio
+1, though you should have return type of Object instead.
Tyler Egeto
I'm still refactoring, but I'm going to go ahead and mark this as the answer. I think it should get me where I'm going. Thanks.
ThatSteveGuy
A: 

In practice, there is an actual getter/setter model in ActionScript. For your case, you could use it like this:

private var _value:*;

public function get value() : * {
  return _value;
}

public function set value(val:*) : void {
  if (typeof val == "int" || typeof val == "boolean" || typeof val == "string") {
    _value = val;
  }
}

This limits the user to setting (per your requirements) the value of this "value" property to data types int, Boolean, or String.

Robusto