3 Replies Latest reply on Mar 2, 2009 3:43 AM by Richard_Abbott

    flag for array value and Multipule array

    rahimhaji Level 1
      Dear Friends,

      i need to create an array, inside that array, each element should have a flag or another set of array how can i create this in Flex. iam new to flex.

      for example i need to create the following structure or algorithm:

      var ar:Array = [one, two, thre];

      one.selected = true;
      two.selected = false;
      thre.selected = true;

      one.username = "ssss";
      two.username = "bbb";
      thre.username = "cccc"

      pls help me my friends.. i got struct up..

      Thanks and Regards,
      Syed Abdul Rahim
        • 1. Re: flag for array value and Multipule array
          Richard_Abbott Level 3
          Syed Abdul Rahim,
          what you are already doing looks on the right track. An Array can hold any kind of object. In your position I would define a class for the actual items the Array holds. The base class for your custom class would obviously depend what kind of functionality you want - eg is it going to have a visual representation in which case inheriting from Canvas or Sprite would be good, but if not then maybe something lower down the class tree. Have a look at the class hierarchy and see what you need out of the standard set of obejcts, then inherit from that.
          Once you have your class definition, you can make your Array dynamically populated with code like
          var ar: Array;
          var x, y, z: MyClass;
          x = new MyClass(true, "ssss");
          ar.push(x);
          y = new MyClass(false, "bbb");
          ar.push(y)
          etc (this relies on you having made your class, written a suitable constructor, and so on, but all this is well documented in Adobe help). Or you can hold your values in a database, XML file etc and create objects on the basis of that. The first step is identifying a suitable base class and additional data you wish to hold - the object can very easily be extended later if you realise maybe you want to hold an integer value alongside the boolean and string ones.
          Hope that helps,
          Richard
          • 2. Re: flag for array value and Multipule array
            rahimhaji Level 1
            Dear Mr.Richard,

            iam in receipt of ur answer, thanks a lot. What iam trying to do is, iam fetching the details from xml and filling the array. in that array i want to store in a single place more than 1 details: for example my xmls is like that

            <obj>
            <link1>aaa </link1>
            <link2>bbb </link2>
            </obj>

            <obj>
            <link1> </link1>
            <link2>bbb </link2>
            </obj>

            <obj>
            <link1>aaa </link1>
            <link2>bbb </link2>
            </obj>
            i want to store this in to array, in sencond obj, u can see link1 is empty, i want to skip that node, only not null nodes i want to store in to the array.

            for that only i need to store the details like ar: array = [{'aaa',23},{'bbb',43}.,{'ccc',65}] like this multiple details in a single array element...

            pls help me .. i got struct up..

            thanks and Regards,
            Syed Abdul Rahim
            • 3. Re: flag for array value and Multipule array
              Richard_Abbott Level 3
              Syed Abdul Rahim,
              Personally I'd still go down the route of having a custom class, with declaration something like
              public class MyClass extends Object
              {
              public link1:String;
              public link2:String;
              // any other members
              }
              To do this really properly you'd make the members private or protected and have public access methods, but let's overlook that for now!
              Then in your main application in the place where you have loaded your XML file you'd do something like:
              iterate through all XML children
              {
              if link1 for the specific XML child is not null then
              {
              create and populate instance of MyClass
              arr.push(the new instance)
              }
              else
              {
              // do nothing or maybe log the incident
              }
              }
              I'm sure you get the principle.
              Another way to do it, which avoids moving the XML content into a differently structured array, would be to have a second XML or XMLList object. As you read through the original XML then if the child meets your condition (link1 not null) then add it into the new XML, else just skip to the next one.
              You can then use the generated XML or XMLList to feed directly into data components such as grids, combo etc.
              I realise this is a bit general, but it's hard to be more specific without knowing how you will use the data. I do also think there are several possible solutions and other aspects of the problem may lead you to choose one over another.
              All the best, Richard