views:

27

answers:

1

I am trying to declare private variables that will be used elsewhere in my flex mxml main application. But I need to only create the variables when their is an equal number in my xml file.

My xml snippet looks like this:

<POIs>
    <location>
            <name>jane</name>
            <lat>12345</lat>
            <long>12345</long>
            <iconFile>jane_icon.gif</iconFile>
            <imageFile>jane_img.swf</imageFile>
            <motion>no</motion>
    </location>
    <location>
            <name>john</name>
            <lat>23456</lat>
            <long>23456</long>
            <iconFile>john_icon.gif</iconFile>
            <imageFile>john_img.swf</imageFile>
            <motion>no</motion>
    </location>
</POIs>

My main flex mxml file looks like this:

//------------------------------------------------------------------------------0
private var locPointMarker0:PointMarker;
private var locPointMarker0_lat:Number;
private var locPointMarker0_long:Number;

//------------------------------------------------------------------------------1
private var locPointMarker1:PointMarker;
private var locPointMarker1_lat:Number;
private var locPointMarker1_long:Number;

//------------------------------------------------------------------------------2
private var locPointMarker2:PointMarker;
private var locPointMarker2_lat:Number;
private var locPointMarker2_long:Number;

//------------------------------------------------------------------------------3
private var locPointMarker3:PointMarker;
private var locPointMarker3_lat:Number;
private var locPointMarker3_long:Number;

I don't want to declare 2000 private variables when my xml might only have 500 locations.

Here is how my xml is being added to my flex app:

public function getXMLData():void
{   
    var locXMLLoader:URLLoader = new URLLoader(locXMLURL);
    locXMLLoader..addEventListener(Event.COMPLETE, xmlLoaded);  
    function xmlLoaded(e:Event):void
    {
        try
        {
            locXML = XML(locXMLLoader.data);
            locXML.location.(parseLocXML(
                valueOf().name.toString(),
                parseFloat(valueOf().lat.toString()),
                parseFloat(valueOf().long.toString()),
                       valueOf().iconFile.toString(),
                       valueOf().imageFile.toString(),
                       valueOf().motion.toString()));
        } catch(e:Error)
        {
            trace("XML Load Error:  " + e.message)
            return;
        }
    }
}

public function parseLocXML(nodeName:String, lat:Number, 
            long:Number, iconFile:String, 
            imageFile:String, motion:String):void

{   
    if (XMLpCounter==0)
    {
    addMarkers(); ///this is just one of the functions that requires the private vars
    }
    XMLpCounter++;
}

I tried the similar code below, but it doesn't work because locXML isn't defined really till its is parsed.

//------------------------------------------------------------------------------1
if(locXML.location.length()>=1){
    trace("1 has been added as a variable");
    private var locPointMarker1:PointMarker;
    private var locPointMarker1_lat:Number;
    private var locPointMarker1_long:Number;}
            //------------------------------------------------------------------------------2
if(locXML.location.length()>=2){
    trace("2 has been added as a variable");
    private var locPointMarker2:PointMarker;
    private var locPointMarker2_lat:Number;
    private var locPointMarker2_long:Number;}

Is it possible to wait until the xml has loaded before declaring these variables?

+1  A: 

Have you thought about an array of locations. Your 2000 variables make my head hurt.

Location.as:

package my.locations.util    {
    public class Location {
        public var latitude;
        public var longitude;
        public var name;
        //... others, constructor
    }
 }

and

 var locations:Array = [];
 var locationsXML:XML = //...
 for (var locationXML:XML in locationsXML.location) {
     var location:Location = new Location(locationXML.latitude, locationXML.longitude, ...);
     locations.push(location);
 }
Michael Brewer-Davis
I don't know much about arrays. I'll look into this and see if I can't get it to work. Thank you...Actually, I don't fully understand classes either. I'm new to flash programming...took on a large project and have been learning along the way.
Phil
would you do me a favor and rename those so that they match my XML? I don't know how to apply this code to my purpose. Arrays aren't making sense to me.
Phil
this is the correct answer. any time you find yourself writing "myvar1". "myvar2", you should create a value object for it and store them in an array. Otherwise what if you needed to add 9 more "lat/long/name" triplets? You'd have to edit your class, and you don't want to do that :)
viatropos