Top RIA blogs award

Top RIA blogs

Confessions of an Flash Addict Rotating Header Image

Tracing XMLList within a switch statement returns null

Just found really nasty bug in Flash; When using XML Filtering within a switch statement it will fail when assigning that value to an XMLList variable and trace null. (Even though there is data)

When you declare the variable outside/above of the switch it does work. What can also be done is assigning the variable to a function that does the filtering.

Apparently you cant assign and set a variable on the same line within a switch because of a scoping issue. Also checked JIRA, Adobe’s BUG database, and it is already filed :)

Here are some examples:

//FAILS!
switch(someName) {
	case "myName":
		trace(_xml.myItems.(@id == "1")) // traces the inside of XMLList
		var items:XMLList = _xml.myItems.(@id == "1");
		trace( items ); // traces null
	break;
}
// This will give us a TypeError: Error #1010: A term is undefined and has no properties. 

//SUCCESS!
var items:XMLList;
switch(someName) {
	case "myName":
		trace(_xml.myItems.(@id == "1")) // traces the XMLList
		items = _xml.myItems.(@id == "1");
		trace( items ); // traces the inside of XMLList
	break;
}

//ALSO SUCCESS!
var items:XMLList;

switch(someName) {
	case "myName":
		trace(_xml.myItems.(@id == "1")) // traces the XMLList
		items = getFilteredXML(_xml.myItems, "id", "1");
		trace( items ); // traces the inside of XMLList
	break;
}

function getFilteredXML(pList:XMLList, pAttribute:String, pValue:String):XMLList {
	return pList.(attribute(pAttribute) == pValue);
}

Hope this saves somebody else a couple of hours bughunting … :)

3 Comments on “Tracing XMLList within a switch statement returns null”

  1. #1 gerb
    on Jul 3rd, 2009 at 4:04 pm

    It did. Thank you very much!

  2. #2 Rafal
    on Sep 17th, 2009 at 8:51 pm

    Where is You temple?
    I will pray to You starting from Yesterday!

  3. #3 Sidney de Koning
    on Sep 17th, 2009 at 9:59 pm

    Its in the North ;)

Leave a Comment