Re: [flexcoders] Re: Simplfying Repetitive Code
Hi Guys, This discussion inspired me to write an article about the dynamic elements of the language. It took a while to finish but after nearly a month I found enough time to wrap it up. http://tiborballai.com/blog/2010/1/11/dynamic-elements-of-the-actionscript-3-language.html Tibor. www.tiborballai.com invertedspear wrote: Tibor, I have been needing something like 'this["string"+var]' for awhile. Would have saved me a lot of time to know about that sooner. Thanks. ~Mike
[flexcoders] Re: Simplfying Repetitive Code
Tibor, I have been needing something like 'this["string"+var]' for awhile. Would have saved me a lot of time to know about that sooner. Thanks. ~Mike
[flexcoders] Re: Simplfying Repetitive Code
private var seriesArray:Array = [series1, series2, series3, series4, series5 ];// etc. private function changeSeries():void { for each (var series:Object in seriesArray) { series.yField = 'male'; } } I haven't tried to compile it and run but it should work. --- In flexcoders@yahoogroups.com, "AJC2357" wrote: > > Hi all, > > If I have a function like this. > > private function changeSeries(): void { > series1.yField = 'male'; > series2.yField = 'male'; > series3.yField = 'male'; > series4.yField = 'male'; > series5.yField = 'male'; > series6.yField = 'male'; > series7.yField = 'male'; > series8.yField = 'male'; > series9.yField = 'male'; > series10.yField = 'male'; > } > > Is there an easy way to simplify it? Something like > > private function changeSeries(): void { > "something to indicate i equaling zero through ten" > series[i] = 'male'; > } > > Any code help would be much appreciated! > > Alex >
[flexcoders] Re: Simplfying Repetitive Code
You are welcome. Be careful though, if you are using the array access notation, the compiler won't be able to catch any mistakes you might make. For instance, this["series11"].yField = 'male'; will compile without errors, even if you don't have a series11 property, but you will receive a run-time error when the code is executed. Tibor. www.tiborballai.com --- In flexcoders@yahoogroups.com, "AJC2357" wrote: > > Thanks so much, Tibor. > > This structure - this["series"+i] - was what I needed! > > --- In flexcoders@yahoogroups.com, "Tibor" wrote: > > > > Hi Alex, > > > > You can use a for loop. > > > > for (var i:uint=1; i<=10; i++){ > > this["series"+i].yField = 'male'; > > } > > > > Hope this helps, > > > > Tibor. > > > > www.tiborballai.com > > > > --- In flexcoders@yahoogroups.com, "AJC2357" wrote: > > > > > > Hi all, > > > > > > If I have a function like this. > > > > > > private function changeSeries(): void { > > > series1.yField = 'male'; > > > series2.yField = 'male'; > > > series3.yField = 'male'; > > > series4.yField = 'male'; > > > series5.yField = 'male'; > > > series6.yField = 'male'; > > > series7.yField = 'male'; > > > series8.yField = 'male'; > > > series9.yField = 'male'; > > > series10.yField = 'male'; > > > } > > > > > > Is there an easy way to simplify it? Something like > > > > > > private function changeSeries(): void { > > > "something to indicate i equaling zero through ten" > > > series[i] = 'male'; > > > } > > > > > > Any code help would be much appreciated! > > > > > > Alex > > > > > >
[flexcoders] Re: Simplfying Repetitive Code
Thanks so much, Tibor. This structure - this["series"+i] - was what I needed! --- In flexcoders@yahoogroups.com, "Tibor" wrote: > > Hi Alex, > > You can use a for loop. > > for (var i:uint=1; i<=10; i++){ > this["series"+i].yField = 'male'; > } > > Hope this helps, > > Tibor. > > www.tiborballai.com > > --- In flexcoders@yahoogroups.com, "AJC2357" wrote: > > > > Hi all, > > > > If I have a function like this. > > > > private function changeSeries(): void { > > series1.yField = 'male'; > > series2.yField = 'male'; > > series3.yField = 'male'; > > series4.yField = 'male'; > > series5.yField = 'male'; > > series6.yField = 'male'; > > series7.yField = 'male'; > > series8.yField = 'male'; > > series9.yField = 'male'; > > series10.yField = 'male'; > > } > > > > Is there an easy way to simplify it? Something like > > > > private function changeSeries(): void { > > "something to indicate i equaling zero through ten" > > series[i] = 'male'; > > } > > > > Any code help would be much appreciated! > > > > Alex > > >
[flexcoders] Re: Simplfying Repetitive Code
simple for loop would do it: for(var i:int=1; i<=10;i++){ } --- In flexcoders@yahoogroups.com, "AJC2357" wrote: > > Hi all, > > If I have a function like this. > > private function changeSeries(): void { > series1.yField = 'male'; > series2.yField = 'male'; > series3.yField = 'male'; > series4.yField = 'male'; > series5.yField = 'male'; > series6.yField = 'male'; > series7.yField = 'male'; > series8.yField = 'male'; > series9.yField = 'male'; > series10.yField = 'male'; > } > > Is there an easy way to simplify it? Something like > > private function changeSeries(): void { > "something to indicate i equaling zero through ten" > series[i] = 'male'; > } > > Any code help would be much appreciated! > > Alex >
[flexcoders] Re: Simplfying Repetitive Code
Hi Alex, You can use a for loop. for (var i:uint=1; i<=10; i++){ this["series"+i].yField = 'male'; } Hope this helps, Tibor. www.tiborballai.com --- In flexcoders@yahoogroups.com, "AJC2357" wrote: > > Hi all, > > If I have a function like this. > > private function changeSeries(): void { > series1.yField = 'male'; > series2.yField = 'male'; > series3.yField = 'male'; > series4.yField = 'male'; > series5.yField = 'male'; > series6.yField = 'male'; > series7.yField = 'male'; > series8.yField = 'male'; > series9.yField = 'male'; > series10.yField = 'male'; > } > > Is there an easy way to simplify it? Something like > > private function changeSeries(): void { > "something to indicate i equaling zero through ten" > series[i] = 'male'; > } > > Any code help would be much appreciated! > > Alex >