i am using get and setters in my as3 code to edit values of an other class (because those variables are shared) i dont like to put stage.sharedVar.isScrabble in my code every time to change a variable so i used get/set functions
see below
private function get isScrabble(){return stage.sharedVar.isScrabble;}
private function set isScrabble(val){stage.sharedVar.isScrabble = val;}
private function get superGrid(){return stage.sharedVar.superGrid}
private function set superGrid(val){stage.sharedVar.superGrid = val;}
private function get gridSize(){return stage.sharedVar.gridSize}
private function set gridSize(val){stage.sharedVar.gridSize = val}
private function get blokDefaultWidth(){return stage.sharedVar.blokDefaultWidth}
private function set blokDefaultWidth(val){stage.sharedVar.blokDefaultWidth = val}
private function get blokDefaultHeight(){return stage.sharedVar.blokDefaultHeight}
private function set blokDefaultHeight(val){stage.sharedVar.blokDefaultHeight = val}
as you van see it has a lot of duplicate code every time the "return stage.sharedVar." and the "stage.sharedVar."+ the value + " = val" is constantly comming back.
i was wondering is there a other way of creating these get/sets? something like?:
private function get variable1(){getValue("hisOwnFunctionName")}
private function set variable1(val){setValue("hisOwnFunctionName")}
and so on??
thanks, Matthy
If I understand your question, you just want to get and set multiple properties on an Object. Depending on your situation you could try:
private function setProperty(name:Object,value:Object):void {
stage.sharedVar[name]=value;
}
private function getProperty(name:Object):Object {
return stage.sharedVar[name];
}
private function example():void {
setProperty("foo","bar");
trace(getProperty("foo")); //prints: bar
}
These functions will let you set and access the properties you want, and you wont have to keep changing the functions. It does mean that if you change superGrid to be something else, you can't just change the function, you have to change everywhere you use the get and setProperty. But it does mean that you don't have to keep writing new functions.
Hope this helps.