-
Returning An Array After For Loop Has Finished
I'm wondering how I can return the value of an array that is being created inside a for loop?
I have created a class that is using ZendAmf to grab page titles out of a database. After the connection is made I put the page titles into an array with a for loop. I would like the class to return the value of the completed array.
I have tried using a public array variable and a getter, but I'm never going to get the correct value until the for loop finishes. So how would I go about getting the array variable after the loop has completed?
I'd rather not set a listener that listens for a boolean inside the for loop. I'm hoping there is a better and more proper way. Thanks.
_______
What happens to your lap when you stand up?
My Site - HexLuv.com
-
I'm a little unclear on the setup - but your for loop should run to completion before any other code is executed - so I'm guessing that you just need an alert to your outside code when the array is compiled? If that's the case just put a dispatchEvent after your loop completes and have your external classes use the getter method then (or you could make a custom event and pass the array directly as a rider).
-
Originally Posted by neznein9
I'm a little unclear on the setup - but your for loop should run to completion before any other code is executed - so I'm guessing that you just need an alert to your outside code when the array is compiled? If that's the case just put a dispatchEvent after your loop completes and have your external classes use the getter method then (or you could make a custom event and pass the array directly as a rider).
Go Ducks! (Unless you happen to be a Beaver...if that's the case I'm sorry).
I'll explain a little more.
I have a "nav" class, along with a few others, that need to make use of an array of page titles. I thought it would be easiest to create a class that would return said array. I agree that we should wait for the array to complete. In this case, that is all the class is really doing. Here is the gist.
Nav class:
PHP Code:
//Instantiate page title class
_pageTitles = new PageTitles(); //This gets things going, but if I immediately call the getter...
var _tempArray = _pageTitles.getPageTitles; //Setting a temporary array to the getter
trace(_tempArray); //This will return null, because the array hasn't finished.
PageTitles classsimplified)
PHP Code:
private function _arrayCreation():void {
var _array:Array = new Array();
for (var i:uint = 0; i < 10; i++) {
_array.push("BestArrayEver" + i)
}
}
public function get getPageTitles():Array {
return _array;
}
I obviously left a lot of stuff out, but this is all we need to solve the problem. I'd be interested to know more about the dispatchEvent and how I could make use of that. I'm all self taught and sometimes the "common knowledge" stuff flys over my head. Thanks in advance.
_______
What happens to your lap when you stand up?
My Site - HexLuv.com
-
Hahah! Thanks for the Ducks lovin' - much appreciated
Alright - the problem is that all your code runs at once (all in one frame - probably the first)...you need to split that so your call out to the server happens immediately and everything dependent on that is just in standby mode until the array is ready.
So - when you create the PageTitles class, give it an event listener rather than calling for the array:
PHP Code:
var _pageTitles:PageTitles = new PageTitles();
_pageTitles.addEventListener('arrayComplete', doStuff);
private function doStuff(e:Event):void{
// it's been a couple frames but now the array is ready...
trace(_pageTitles.getPageTitles);
}
Now that code is just hanging out until it hears from _pageTitles, so inside you need to fire off a custom event:
PHP Code:
private function _arrayCreation():void {
var _array:Array = new Array();
for (var i:uint = 0; i < 10; i++) {
_array.push("BestArrayEver" + i)
}
dispatchEvent(new Event('arrayComplete'));
}
Now your code will run over a few frames but everything is guaranteed to happen in order. And if you need to, you can use that technique again in the outer class to let everything else in the program know when it's alright to initialize.
-
Sweet! Thanks. So easy....as long as you know what your doing.
If your ever in Salt Lake City, I'm the guy with the "Go Duks" plates driving around. Cheers!
_______
What happens to your lap when you stand up?
My Site - HexLuv.com
-
Also, your _array scope is incorrect. You need to declare _array at the class level and NOT inside your _arrayCreation function. If it's declared in the _arrayCreation function then that function will be using a variable local to its scope, and the getter will have no access to it.
-
Originally Posted by 5TonsOfFlax
Also, your _array scope is incorrect. You need to declare _array at the class level and NOT inside your _arrayCreation function. If it's declared in the _arrayCreation function then that function will be using a variable local to its scope, and the getter will have no access to it.
Thanks 5Tons. In my actual .as file it was declared at the class level, I was just too lazy to write out the entire package/class/constructor hoopla in this thread. My bad. But, it's all fixed so that is good.
_______
What happens to your lap when you stand up?
My Site - HexLuv.com
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|