Home  |  About  | Last |  Submit  |  Contact
AllQuests.com




Previous Question:  preload html with flash  Flash General HelpNext Question:  hitTests acting weird  Flash ActionScript
Question creating variable from two others ( Flash Kit Community Forums Flash ActionScript )
Updated: 2010-07-21 06:00:02 (4)
creating variable from two others

Hi,

I am trying to populate a combobox(ComboCities) to display cities dependant on the selection of another combobox (ComboCountries).
Since the list of countries come from a database, and expand over time, I want to use the CountryID-value to determine which array must be read to
populate the ComboCities.

Currently I do the following:

//populating country data
countries="England|France|Germany";
countriesID="1|2|3";

//loading all cities: note-> every arrayname contains the countryID
cityname1="London|Leeds|Liverpool|Glasgow";
citiesID1="11|12|13|14";
cityname2="Paris|Lyon|Marseille|Lille";
citiesID2="15|16|17|18";

//function to populate the ComboCities
function getcities ()
{
countryid=CombCountries.getValue();
ComboCities.removeAll();
NewIDs="citiesID"+countryid;
NewName="cityname"+countryid;

listcityids=NewIDs.split("|");
listcitynames=NewName.split("|");
for (i=0; i<listcityids.length;i++) {ComboCities.addItemAt(i,listcitynames[i], listcityids[i]);}
}

Somehow the arrays 'listcityids' and 'listcitynames' are not created. I found a workaround that will create these arrays by
changing the following codepart:

NewIDs=eval("citiesID"+countryid);
NewName=eval("cityname"+countryid);

So my question really is:
Why I had to use the eval() function for this?(why does it technically changes the logic of the code)

I hope someone can explain it, because I had to add code, without knowing the true purpose.

Kind regards
Patrick

Answers: creating variable from two others ( Flash Kit Community Forums Flash ActionScript )
creating variable from two others

please, anyone?

cuboctahedron

creating variable from two others

here eval is evaluating the vairable rather than just setting a string value.

this code:
NewIDs="citiesID"+countryid;
sets the variable NewIDs to a string (the concat of "citiesID"+countryid).
eg if countryid=3 NewIDs="citiesID3".

this code:
NewIDs=eval("citiesID"+countryid);
sets the variable NewIDs to the value of the variable "citiesID"+countryid.
eg if countryid=3 NewIDs = the value of the variable citiesID3.

hope thats clear

gaz_b

creating variable from two others

Thankx: that was clear to me

cuboctahedron

creating variable from two others

Quote:
Originally posted by cuboctahedron
Thankx: that was clear to me
nice one.

just a tip that you can use array notation instead of eval eg:

a = 7
var7= "Hello World!"

b = eval("var" + a)
c = this["var"+a]

trace (b); // outputs "Hello World!"
trace (c); // outputs "Hello World!"

also eval() works a little differently in MX compared to Flash 5 .

hope that helps

gaz_b

Previous Question:  preload html with flash  Flash Kit Community Forums  Flash General HelpNext Question:  hitTests acting weird  Flash Kit Community Forums  Flash ActionScript

- Source: creating variable from two others Flash Kit Community Forums Flash ActionScript
- Previous Question: preload html with flash Flash Kit Community Forums Flash General Help
- Next Question: hitTests acting weird Flash Kit Community Forums Flash ActionScript