4 Replies Latest reply on Feb 2, 2011 9:45 PM by Wrataxas

    Initializing a Vector of my objects

    Wrataxas Level 2

      I have a class like this:

       

      public class BakedGood

      {

           public var name:String;

           public var color:uint;

           public var hasFilling:Boolean;

      }

       

      and I want to initialize a Vector of these, something like:

       

      var wares:Vector.<BakedGood> = new <BakedGood>

           [

                { name:"muffin", color:0x123456, hasFilling:false },

                { name:"pie", color:0x654321, hasFilling:true },

                { name:"cake", color:0x121212, hasFilling:false }

           ];

       

      This doesn't work because the { } creates objects of type Object and not of BakedGood.  I haven't been able to divine any other syntax that would let me do this.  Is it possible?  Does anyone know how?

       

      Thanks.

        • 1. Re: Initializing a Vector of my objects
          Flex harUI Adobe Employee

          There is no literal syntax that is shorthand for creating class instances.

           

          Think ahead and consider whether your data will be declared in code or will

          come from a server.  Usually it comes from a server and some other logic

          will create the class instances.

           

          If you want to create a bunch of classes instances in code, I often add a

          constructor:

           

          public class BakedGood

          {

               public function BakedGood(name:String = null,

                                  color:uint = 0;

                                  hasFilling:Boolean = false)

              {

                  this.name = name;

                  this.color = color;

                  this.hasFilling = hasFilling;

              }

           

               public var name:String;

               public var color:uint;

               public var hasFilling:Boolean;

          }

           

          Then create them like this:

           

          var wares:Vector.

               [

                    new BakedGood("muffin", 0x123456, false ),

                    new BakedGood("pie", 0x654321, true ),

                    new BakedGood("cake", 0x121212, false )

               ];

          • 2. Re: Initializing a Vector of my objects
            Wrataxas Level 2

            Not coming from a server in this case; just embedded in code.

             

            Thanks for the great alternative.

            • 3. Re: Initializing a Vector of my objects
              GordonSmith Level 4

              You can also use MXML:

               

              <fx:Declarations>

                  <fx:Vector id="wares" type="BakedGood">

                      <BakedGood name="muffin" color="0x123456" hasFilling="false"/>

                      <BakedGood name="pie" color="0x654321" hasFilling="true"/>

                      <BakedGood name="cake" color="0x121212" hasFilling="false"/>

                  </fx:Vector>

              </fx:Declarations>

               

              Gordon Smith

              Adobe Flex SDK Team

              1 person found this helpful
              • 4. Re: Initializing a Vector of my objects
                Wrataxas Level 2

                I didn't know about that option either.  Thanks!