Losing scope

ok. i’m banging my head against the wall now. can someone please explain to me why i am losing scope on an array that i define as a private member in a class??

i am able to access it from a couple of different functions but in one place inparticular it keeps coming up “undefined”.

what causes scope to be lost in a function for an array that is defined as a private class member? please help me understand. at least from there i’ll be able to figure out how to approach fixing it properly.

thanks. fumeng.

yeah, some code would help

ok. thank you, senocular. i appreciate you taking a look.

here’s my class file with some initial properties set. the array i’m having trouble accessing consistently is ArrDate.

class EventsCalendar extends mx.core.UIComponent {
   // Private XML Properties
   private var myXML:XML;
   private var rootNode;
   private var ArrDate:Array;
   private var ThisNode;

now here’s my parseXML function which populates ArrDate:

function parseXML() {
   // reference the root node so we can access its children
   rootNode = myXML.childNodes[0];
   // set up an array so we can load the attributes into it
   ArrDate = new Array();
   // set up an array for the child nodes because we don't want to access 
   // them in the loop directly -- it's faster this way
   var childNodes:Array = rootNode.childNodes;
   // get the data
   for (var i=0; i < childNodes.length; i++) {
	ThisNode = rootNode.childNodes*; 
	var bs:Array = ThisNode.attributes;
	ArrDate* = ThisNode;

now here’s a loop within a totally different function later on in my program which tries to access ArrDate. it succeeds in the ‘if’ statement but not in the onRelease handler:

//  Highlight and Hyperlink XML Dates
var numSpecial:Number = ArrDate.length;
for(var k=0;k<numSpecial;k++){
   if(ArrDate[k].attributes.day == j){ // if we've got a match..
	// Assign Background Color
	dateClip["thisDate"+(ArrDate[k].attributes.day)].background = true; // here ArrDate[k].attributes.day is traced ok but below is undefined...
	dateClip["thisDate"+(ArrDate[k].attributes.day)].backgroundColor = 0xFFCC33;
	// Use onRelease Handler to getURL
	_root["dateClip"+(ArrDate[k].attributes.day)].onRelease = function() {
		trace(ArrDate[k].attributes.urlPath); // this is the problem area!!! --- this traces 'undefined'

so there it is. how can i get this trace statement to work:

_root["dateClip"+(ArrDate[k].attributes.day)].onRelease = function() {

thanks for taking a look, senocular.


thats because you left the scope of you class instance and went into the scope of some movieclip in the onRelease where ArrDate is not accessible (as its not defined there).

you’ll just need to create a variable reference that the onRelease can access, either via a local variable defined directly before the onRelease or by assigning ArrDate to the object which is getting the onRelease defined in it.

i guess what i wasn’t understanding is that i will lose scope as soon as i go into that event handler. so, essentially i switched scope. so now i could do s/thing like this:

var app = this;

but i suppose i’ll lose ‘k’ when i go in here as well so i could store a reference to that in an object, too.

thanks senocular for helping me gain a better understanding of this.