Ultrashock Forums > Blogs > Whirled Views
Finally, I understand the utility of getter/setters
Member Blogs
 
Join me for a different perspective on Flash/Actionscript programming: a perspective saturated with hyperbole and couched in precocious vernacular.
With diligent perseverance and a little luck, this blog could become the center of a radical new view of the web.
'Course I'd settle for entertaining and/or insightful.

Welcome to my Whirled View!
Bookmark and Share!
Rate this Entry

Finally, I understand the utility of getter/setters

Posted 2008-06-11 at 20:24 by jaseinatl
Part 1 of ____ : Intro to the Joy of Getter/Setters.
-----------------------------------------------------

Sure, I have written a ton of classes in Actionscript 2.0. But I never really thought about how useful the getter/setter methods are.

I thought that Actionscript used the getter function and a setter function only out of allegiance to some OOP methodology, not because there is a real use for them.

That's mostly because I thought that your getters and setters had to look like this:

Code:
public function get agrip(Void):String {
return (this._agrip);
}
public function set agrip (st:String):Void {
this._agrip=st?st:"Nothing";
}
all so you could say:

Code:
trace (this.agrip+ " is worth fighting over");
well, now I finally get it-------------------------------

The setter function allows you to implement a form of event-handling in an entirely new way. For example, if you create a custom "color" getter/setter pair for a class that extends movieClip, you can fire an event whenever something changes that property.
Code:
public function get color():Number {
return (this._color); //_color is a private variable
}

public function set color(num:Number) {
if (this._color!=num && num!=undefined) {
trace ("aha, I am changing color");
var clr:Color=new Color(this);
clr.setRGB(num);
this._color=num;
}
}
Now, when I say clip.color=0x00ff00; it will trace the message and change it's color.

This is nice because it allows me to do error checking on my private properties before assigning them.
Code:
private var _beanCount:Number;
private var _maxBeans:Number=100;

public function addBeans(num:Number) {
var beans:Number=this._beanCount;
beans+=num;
if (beans>this._maxBeans) {
trace ("too many beans");
} else {
this._beanCount=beans;
}
}
now becomes:
Code:
private var _beanCount:Number;
private var _maxBeans:Number=100;

public function get beans():Number {
return (this._beanCount);
}

public function set beans(num:Number) {
if (this._beanCount+num>this._maxBeans) {
trace ("too many beans");
}
this._beanCount=Math.min(this._beanCount+num, this._maxBeans);
}
and the addBeans function goes away.

Now, to add beans, you can simply call:
Code:
this.beans+=5;
this.beans-=100;
or
Code:
while (this.beans+=5<this._maxBeans) {
trace ("I'm adding 5 beans");
}
trace ("I've reached my limit");
}
This becomes really useful when you want to apply a transformation to a clip without storing the value in a register. For the above example, though I stored the number in this._color, I could have just as easily skipped that step so that I didn't store anything.

Code:
public function set color(num:Number) {
var clr:Color=new Color(this);
clr.setRGB(num);
}
public function get color():Number {
return (-1);
}
Although this is kind of ugly because you don't get the color returned when you ask for clip.color, it still gets the job done.

------------------------------------------------------
 
Recent Blog Entries by jaseinatl