|
-
Why does eval() make a difference to this code snippet?
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
Last edited by cuboctahedron; 11-20-2002 at 06:16 PM.
-
-
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
-
Thankx: that was clear to me
-
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
Last edited by gaz_b; 11-20-2002 at 07:03 PM.
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
|