# \[8\] scope issue

**URL:** <https://forum.kirupa.com/t/8-scope-issue/261004>\
**Category:** flash\
**Created:** [May 21, 2008, 11:20pm UTC](https://forum.kirupa.com/t/8-scope-issue/261004 "2008-05-21T23:20:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![flashcb70](https://avatars.discourse-cdn.com/v4/letter/f/e9bcb4/32.png) [@flashcb70](https://forum.kirupa.com/u/flashcb70)\
**Post date:** [May 21, 2008, 11:20pm UTC](https://forum.kirupa.com/t/8-scope-issue/261004/1 "2008-05-21T23:20:40Z")

</div>

I’ve build a Array (DataListArray) and want the Array elements be able to be accessed outside the function they where created in.

The only way I can get it to work is if i do everything within the parseXMLData function.  
Can sombody please help?  
Thanks!

//create a new XML object  
var outputXml = new XML();  
//create varible for xml file url (xml url needs to be on same server)  
var outputXmlUrl:String = “xmlDocs/testData1.xml”;  
//ignore whitespace in the file  
outputXml.ignoreWhite = true;  
//load the xml file  
outputXml.load(outputXmlUrl);  
//call the parseXMLData function when the XML file is loaded  
outputXml.onLoad = parseXMLData;  
//make sure the data is loaded and usable  
function parseXMLData(success:Boolean):Void {  
if (success && this.status == 0) {  
var DataListArray = new Array();  
var DataListArray = this.firstChild.childNodes;  
// populate Array ( every XML \<data\> Node is a new array element)  
trace(DataListArray);  
} else {  
trace(“Problem loading XML data”);  
trace("The error code is "+this.status);  
}  
}  
// assign values to text fields  
com2servTcp\_txt = DataListArray[0].firstChild.firstChild.nodeValue;  
com2servUpd\_txt = DataListArray[1].firstChild.firstChild.nodeValue;  
lineLat\_txt = DataListArray[5].firstChild.firstChild.nodeValue;  
lineJit\_txt = DataListArray[6].firstChild.firstChild.nodeValue;  
linePac\_txt = DataListArray[7].firstChild.firstChild.nodeValue;  
com2clTcp\_txt = DataListArray[2].firstChild.firstChild.nodeValue;  
com2clUpd\_txt = DataListArray[3].firstChild.firstChild.nodeValue;  
sipFirewall\_txt = DataListArray[4].firstChild.firstChild.nodeValue;

---

<div class="post-metadata">

**Author:** ![shotymac](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@shotymac](https://forum.kirupa.com/u/shotymac)\
**Post date:** [May 22, 2008, 12:59am UTC](https://forum.kirupa.com/t/8-scope-issue/261004/2 "2008-05-22T00:59:00Z")

</div>

Simply define your Array Instance outside of the function see below: Changes are in bold

//create a new XML object  
var outputXml = new XML();  
**var DataListArray = new Array();**  
//create varible for xml file url (xml url needs to be on same server)  
var outputXmlUrl:String = “xmlDocs/testData1.xml”;  
//ignore whitespace in the file  
outputXml.ignoreWhite = true;  
//load the xml file  
outputXml.load(outputXmlUrl);  
//call the parseXMLData function when the XML file is loaded  
outputXml.onLoad = parseXMLData;  
//make sure the data is loaded and usable  
function parseXMLData(success:Boolean):Void {  
if (success && this.status == 0) {  
**DataListArray = this.firstChild.childNodes;**  
// populate Array ( every XML \<data\> Node is a new array element)  
trace(DataListArray);  
} else {  
trace(“Problem loading XML data”);  
trace("The error code is "+this.status);  
}  
}  
// assign values to text fields  
com2servTcp\_txt = DataListArray[0].firstChild.firstChild.nodeValue;  
com2servUpd\_txt = DataListArray[1].firstChild.firstChild.nodeValue;  
lineLat\_txt = DataListArray[5].firstChild.firstChild.nodeValue;  
lineJit\_txt = DataListArray[6].firstChild.firstChild.nodeValue;  
linePac\_txt = DataListArray[7].firstChild.firstChild.nodeValue;  
com2clTcp\_txt = DataListArray[2].firstChild.firstChild.nodeValue;  
com2clUpd\_txt = DataListArray[3].firstChild.firstChild.nodeValue;  
sipFirewall\_txt = DataListArray[4].firstChild.firstChild.nodeValue;

---

<div class="post-metadata">

**Author:** ![flashcb70](https://avatars.discourse-cdn.com/v4/letter/f/e9bcb4/32.png) [@flashcb70](https://forum.kirupa.com/u/flashcb70)\
**Post date:** [May 22, 2008, 6:00am UTC](https://forum.kirupa.com/t/8-scope-issue/261004/3 "2008-05-22T06:00:00Z")

</div>

That works, but I also want the populated Array accessible outside the parseXmlData function. The way it is right now the only way to assign the values to the text fields is inside the parseXmlData function.  
Any ideas?  
Thanks!

---

<div class="post-metadata">

**Author:** ![randomagain](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@randomagain](https://forum.kirupa.com/u/randomagain)\
**Post date:** [May 22, 2008, 8:47am UTC](https://forum.kirupa.com/t/8-scope-issue/261004/4 "2008-05-22T08:47:23Z")

</div>

if you can’t get complex code to work, do it the normal basic way 😛

---

<div class="post-metadata">

**Author:** ![shotymac](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@shotymac](https://forum.kirupa.com/u/shotymac)\
**Post date:** [May 22, 2008, 6:46pm UTC](https://forum.kirupa.com/t/8-scope-issue/261004/5 "2008-05-22T18:46:27Z")

</div>

[quote=flashcb70;2329504]That works, but I also want the populated Array accessible outside the parseXmlData function. The way it is right now the only way to assign the values to the text fields is inside the parseXmlData function.  
Any ideas?  
Thanks![/quote]

This is because you are assigning the values within an onload event. The array values will be available outside of your parseXmlData function as long as any other function or object that needs these values makes reference to them after the the loading is complete and the array has been setup within this onload event. If you try to reference the array values outside of a conditional or the onload event the reference may fire off before the values are ready causing an Undefined error. You can easily add some error checking by setting a boolean(true/false) value once the array is setup and all values are populated. You can then use this boolean value to trigger your other functions that require the use of the array values outside of the onload event and the parseXmlData function. a simple example is below, but you could get as detailed as you want with it. New Code is in bold.

**var arrayReady:Boolean = false;**  
outputXml.onLoad = parseXMLData;  
//make sure the data is loaded and usable  
function parseXMLData(success:Boolean):Void {  
if (success && this.status == 0) {  
DataListArray = this.firstChild.childNodes;\*\*  
arrayReady = true;  
\*\* // populate Array ( every XML \<data\> Node is a new array element)  
trace(DataListArray);  
} else {  
trace(“Problem loading XML data”);  
trace("The error code is "+this.status);  
**arrayReady = false;**  
}  
}  
// assign values to text fields  
**this.onEnterFrame = function() {  
if(arrayReady){  
com2servTcp\_txt = DataListArray[0].firstChild.firstChild.nodeValue;  
}else {  
\*\*\*\* com2servTcp\_txt =“sorry your array has not been populated yet”;**  
**}  
}**

Sean  
I also just realized that if com2servTcp\_txt is a text field I am not sure how you set it up as it is not included in your code but to set the text you may want to use the following.  
\*\* com2servTcp\_txt.text=

instead of  
\*\*\*\* com2servTcp\_txt=\*\*
