XML variable in switch() function fails

Hi there,

having simple problem, working in Flash CS3 IDE. This is so basic i wonder wether i made a mistake or i just don’t understand basics of local function variables or what…

simply put: i can trace a value of an XML variable (which was created using .(@name == “value”) search) using trace() statement correctly, however if i create this variable inside switch() which is inside a function, i can’t place the XML content inside the variable to be further used.

okey check this simple code, you’ll understand quickly:


var xml:XML = <menu>
  <item name="one" />
  <item name="two" />
  <item name="three" />
</menu>;
 
function switchFunction( val:int ) {
 switch( val ) {
  case 0: break;
 
  case 1:
   // following throws ERROR (no such property toXMLString):
   // var element = xml.item.(@name == "two").toXMLString();
 
   // so lets see...
   var element = xml.item.(@name == "two");
 
   trace( "(2) "+element ); // (1) undefined
   trace( "(3) "+xml.item.(@name == "two").toXMLString() ); // <item name="two"/>
   trace( "(4) "+xml.item ); // <item name="one"/><item name="two"/><item name="three"/>
   break;
 }
};
 
// inline works
var element = xml.item.(@name == "two").toXMLString();
trace( "(1) "+element ); // <item name="two"/>
 
// inside function switch() from variable doesnt work
switchFunction( 1 );

Especcially (2) and (3) is interresting… same data, only once assigned to a variable - becomes untraceable…

Anybody met with this issue? Anybody who could recommend a workaround? I would love to use a variable inside that switch statement.

EDIT: If the variable is declared (inside the function body but) outside the switch() statement, and filled with content inside the switch() statement like:


function switchFunction( val:int ) {
 var element:*; // declared here
 switch( val ) {
  case 0: break;
  case 1:
   element = xml.item.(@name == "two"); // filled here
   ....

… all works okay. Ill use this TEMP solution for the moment although my vars are different data-types, so its gotta be var element:*; (ouch)

EDIT2: if you really need to use custom variables inside that switchFunction() (and dislike using Object), use multiple if-s rather than switch().