Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variables in paths

Status
Not open for further replies.
Dec 24, 2001
857
GB
I'm creating an app which reads hundreds of values out of an XML file, stores them in seperate arrays depending on the tags, populates a list with the relevant values depending on what button is clicked and finally displays all the attributes of the value which is clicked in seperate text boxes.

Certain values in each array link to other values in another array, so for example, the SYSTEM array has the ID of the person who is responsible for that system. Clicking a button displays the details of that person (and it then goes further to give a button to display everyone in their department etc etc).

Now to the problem, and to keep things simple I'll just stick to one array (peopleDetails) because if there is a solution for this I can implement it on the rest!

There are several text fields displayed when you click one of the people in the list and a few of these text fields have buttons which link elsewhere. The function which runs on the majority of these buttons is identical except for part of the path. Heres an example of the one which displays all of the people in the same department as the person clicked in the list:

Code:
function displayPeopleDepartment(){
	cwipList.removeAll();
	currDep = _root.peopleTexts.txtPersonDepartment.text;
	n=0;
	for (i=0; i<personDetails.length; i++){
		if (personDetails[i].personDepartment == currDep){
			cwipList.addItem(personDetails[i].personName,i);
			n++;
		};
	};
	cwipList.sortItemsBy("label", "ASC");
	txtCurrentList.text = currDep + " (" + n + ")";
};

and heres one which displays all of the people in the same location of that person:

Code:
function displayPeopleLocation(){
	cwipList.removeAll();
	currLoc = _root.peopleTexts.txtPersonLocation.text;
	n=0;
	for (i=0; i<personDetails.length; i++){
		if (personDetails[i].personLocation == currLoc){
			cwipList.addItem(personDetails[i].personName,i);
			n++;
		};
	};
	cwipList.sortItemsBy("label", "ASC");
	txtCurrentList.text = currLoc + " (" + n + ")";
};

as you can see, they're almost identicle.

cwipList - the list component (obvious really!)
currDep - that var which holds whatever value is in the text box which is later compared to the value in the array
currLoc - that var which holds whatever value is in the text box which is later compared to the value in the array
n - just a counter...its used in a label to show haw many values are in the list
personDetails - the array which stores all the people and their attributes (name, id etc)

What I'm hoping to do is have a variable, in this case I'll call it personInfo, which is set depending on what button is clicked. So if the user clicks on the department button, that variable holds "department", if they click on location it stores "location" etc.

Then when the function runs, instead of where it says
Code:
_root.peopleTexts.txtPersonDepartment.text;
I can hopefully replace txtPersonDepartment with whatever is stored in personInfo.

Any time that I've tried doing this e.g.

_root.peopleTexts.personInfo.text

or

_root.peopleTexts.("txtPerson" + personInfo).texts //where personInfo would be "department" or "contact" etc

I just get an error about it being undefined. Is there any way to use a variable in the path or some sort of work around because I've tried loads of different ways (most of which I can't remember!) and none work.

If any of this is unclear (which I'm sure it is!) ask me whatever you need to know and I'll answer. Bear in mind that the app works as it should - this is just a question to try to make the app more efficient instead of repeating unneccessary code.

Any help is appreciated.

Thanks
 
I set ' personInfo = "Department" ' on the button and put that bit of code in the function but is just gives me undefined. It would be really useful if there was some way to get this working because I have another function for creating text fields (some values in the SYSTEM array have several extra attributes whereas others don't so I wanted to dynamically create the text fields).

The code for that is:

Code:
function addSystemSourceInventory(i){ //DOESN'T WORK PROPERLY YET
	newX = _root.systemTexts.txtSystemSystemOwnerUid._x
	newY = _root.systemTexts.txtSystemSystemOwnerUid._y + 20;
		for (k=0; k<systemDetails[i].systemSourceName.length; k++){
			_root.systemTexts.createTextField("systemSourceNameText",this.getNextHighestDepth,newX,newY,177,17.4);
			_root.systemTexts.systemSourceNameText.text=systemDetails[i].systemSourceName[k]
			newY = newY + 20;
		}
}

this actually works so far in that it creates a text field for the last attribute.

what i wanted to do with this bit was replace the line:

Code:
_root.systemTexts.createTextField("systemSourceNameText",this.getNextHighestDepth,newX,newY,177,17.4);

with

Code:
_root.systemTexts.createTextField([b]"systemSourceNameText"+k[/b],this.getNextHighestDepth,newX,newY,177,17.4);
or something along those lines, but again it gives me undefined because its obviously the same problem as mention in my first post.

anyway, thanks for the help!
 
Debug - in this case you have to pinpoint what is [tt]undefined[/tt] and what isn't. For example:
[tt]
--
trace("personInfo: "+ personInfo)
trace("_root.peopleTexts.txtPersonDepartment: "+_root.peopleTexts.txtPersonDepartment)
_root.peopleTexts["txtPerson"+personInfo].text
--
[/tt]
Then you can tell according to the outputs. If [tt]personInfo[/tt] is [tt]undefined[/tt] then you know it's a scope issue, etc.

Kenneth Kawamoto
 
I suspect it is the scope problem. Start with [tt]personInfo[/tt]: where are you defining this variable? The variables live in the object where they are defined (the "scope" of the variable) - i.e. [tt]personInfo[/tt] is only accessible within where it is defined. The easiest way to solve this is to create [tt]personInfo[/tt] on the main timeline:

[tt]//Main timeline
var personInfo:String
//[/tt]

Then you can assign/access the variable from anywhere using [tt]_root. personInfo[/tt]:

[tt]//In a function
_root.personInfo = "Department"

//In some other function
currInfo = _root.peopleTexts["txtPerson"+_root.personInfo].text
trace("currInfo: "+ currInfo)
//[/tt]
etc.

Kenneth Kawamoto
 
Hi - sorry about the last post where I said all three were undefined:

trace("personInfo: "+ personInfo) was the only one that worked.

Anyway, I solved the problem using the eval function.

Code:
currInfo = eval("_root.peopleTexts.txtPerson" + personInfo + ".text");

returns the right value.

Thanks for your help though!

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top