A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Accessing a static XML variable from within a switch statement

  1. #1
    Member
    Join Date
    Jan 2009
    Posts
    90

    Question Accessing a static XML variable from within a switch statement

    In the following class

    Code:
    package
    {
    	public class Factory
    	{
    		public static var myXML:XML;
    		
    		public function Factory()
    		{
    		}
    		
    		public static function staticTest(varName:String)
    		{
    		switch(varName)
    			{
    				default:
    				Factory.myXML = <entitydefaults>
    				<entity id='DRONE' points='100'></entity>
    				<entity id='BOMBER' points='1000'>
    				</entity>
    				</entitydefaults>
    				var x1 = Factory.myXML.entity.(@id == 'DRONE');
    				var x2 = Factory.myXML.entity.(@id == 'DRONE');
    				trace('okay');
    			}
    		}
    
    	}
    }
    A break point on the "Okay" line shows that x1 is null but x2 is equal to "<entity id="DRONE" points="100"/>".

    If I take the whole block out of the switch statement, both x1 and x2 are equal to the expected xml.

    Why?

  2. #2
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    try terminating your inline xml with a semicolon
    PHP Code:
    </entitydefaults>; 

  3. #3
    Member
    Join Date
    Jan 2009
    Posts
    90
    Thanks for the suggestion, but that didn't help.

  4. #4
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    try this:
    PHP Code:
    package
    {
        public class 
    Factory
        
    {
            public static var 
    myXML:XML;
            
            public function 
    Factory()
            {
            }
            
            public static function 
    staticTest(varName:String)
            {
            var 
    x1,x2;
            switch(
    varName)
                {
                    default:
                    
    Factory.myXML = <entitydefaults>
                    <
    entity id='DRONE' points='100'></entity>
                    <
    entity id='BOMBER' points='1000'>
                    </
    entity>
                    </
    entitydefaults>
                    
    x1 Factory.myXML.entity.(@id == 'DRONE');
                    
    x2 Factory.myXML.entity.(@id == 'DRONE');
                    
    trace('okay');
                }
            }

        }


  5. #5
    Member
    Join Date
    Jan 2009
    Posts
    90
    Looks like this is actually one of those rare cases where the bug isn't in the code, it's in the AS3 VM:

    http://bugs.adobe.com/jira/browse/ASC-2835

  6. #6

    Testing of XML attribute

    someone else had the same problem with testing for existence of an XML attribute. He thought he could type
    Code:
    var postNodes:XMLList = my_xml2.viewentry.entrydata.(@category != undefined);
    You have to go through the XML to find what your looking for. I came up with this on the timeline.

    Code:
    var myXML:XML = new XML();
    
    function staticTest(varName:String){;
    
    myXML=XML('<entitydefaults><entity id="DRONE" points="100"></entity><entity id="BOMBER" points="1000"></entity></entitydefaults>');
    
    var xmllistamount:int=myXML.entity.length()-1;
    
    for (var i:int = 0; i<=xmllistamount; i++) {
    	
    	var varname:String=myXML.entity[i].@id;
    	
    	switch (varname) {
    		
    		case 'DRONE' :
    			trace(myXML.entity[i].@points);
    			break;
    
    		case 'BOMBER' :
    			trace(myXML.entity[i].@points);
    			break;
    
    		default :
    			trace('okay');
    			break;
    	}
    }
    }
    
    staticTest('o');
    for the origin of the testing for existence of an XML attribute problem go here

  7. #7
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    @justkevin: declaring the variable before the switch (but instantiating in the case... as per the last example i posted) worked for me. and the link you posted says encasing the case statement in curlies is a valid workaround too. but yeah, you're right - it's wierd.

    @onenterframe: i think that's a different issue than the one justkevin is experiencing.
    Last edited by moagrius; 06-01-2009 at 07:29 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center