3 Replies Latest reply on Jan 23, 2009 7:33 PM by Newsgroup_User

    Problem Adding Item of type VO to ArrayCollection

    mtgna
      Hi

      I have problem when trying to add a VO to an ArrayCollection as follows. My latest entry overwrites all my previous entries in the Array.

      model.selectedItem.date = dateInput.text;
      model.selectedItem.accountno = accountnoInput.text;
      model.selectedItem.debit = Number(debitInput.text);
      model.selectedItem.credit = Number(creditInput.text);

      model.accountingdata.addItem(model.selectedItem);

      where
      selecteditem is of type VO
      accountingdata is ArrayCollection



      When I do a trace for the first entry:
      trace(model.accountingdata);
      The result:
      (Array)#0
      [0] (com.accounting.superann.vo::DataVO)#1
      accountno = "776001"
      credit = 233
      date = "01-01-1976"
      debit = 222
      id = 0

      When I do a trace for the second and third entries:

      (Array)#0
      [0] (com.accounting.superann.vo::DataVO)#1
      accountno = "776055"
      credit = 2323
      date = "03-03-2000"
      debit = 2323
      id = 0
      online = false
      [1] (com.accounting.superann.vo::DataVO)#1
      [2] (com.accounting.superann.vo::DataVO)#1

      Could someone please provide an explanation/solution?

      Much appreciated.
        • 1. Re: Problem Adding Item of type VO to ArrayCollection
          Level 7

          "mtgna" <webforumsuser@macromedia.com> wrote in message
          news:glcr0a$kj8$1@forums.macromedia.com...
          > Hi
          >
          > I have problem when trying to add a VO to an ArrayCollection as follows.
          > My
          > latest entry overwrites all my previous entries in the Array.
          >
          > model.selectedItem.date = dateInput.text;
          > model.selectedItem.accountno = accountnoInput.text;
          > model.selectedItem.debit = Number(debitInput.text);
          > model.selectedItem.credit = Number(creditInput.text);
          >
          > model.accountingdata.addItem(model.selectedItem);
          >
          > where
          > selecteditem is of type VO
          > accountingdata is ArrayCollection
          >
          >
          >
          > When I do a trace for the first entry:
          > trace(model.accountingdata);
          > The result:
          > (Array)#0
          > [0] (com.accounting.superann.vo::DataVO)#1
          > accountno = "776001"
          > credit = 233
          > date = "01-01-1976"
          > debit = 222
          > id = 0
          >
          > When I do a trace for the second and third entries:
          >
          > (Array)#0
          > [0] (com.accounting.superann.vo::DataVO)#1
          > accountno = "776055"
          > credit = 2323
          > date = "03-03-2000"
          > debit = 2323
          > id = 0
          > online = false
          > [1] (com.accounting.superann.vo::DataVO)#1
          > [2] (com.accounting.superann.vo::DataVO)#1
          >
          > Could someone please provide an explanation/solution?
          >

          Sounds like you have all of the elements in the ArrayCollection pointing to
          the same Object. Are you calling new DataVO() every time you add a new VO
          to the AC?


          • 2. Re: Problem Adding Item of type VO to ArrayCollection
            mtgna Level 1
            Thanks Amy, I fixed the problem by using a new DataVO and removed reference to the VO in the model, although it's still hard for me to understand how an AC can be pointing to the same VO or have a fixed reference to an existing VO instead of just adding another item.
            • 3. Re: Problem Adding Item of type VO to ArrayCollection
              Level 7

              "mtgna" <webforumsuser@macromedia.com> wrote in message
              news:gldpq1$qq5$1@forums.macromedia.com...
              > Thanks Amy, I fixed the problem by using a new DataVO and removed
              > reference to
              > the VO in the model, although it's still hard for me to understand how an
              > AC
              > can be pointing to the same VO or have a fixed reference to an existing VO
              > instead of just adding another item.

              If you do something like ths:

              //assume stuff is a collection of data you're populating
              for ( var i=0; i<stuff.length; i++) {
              myVO.prop1=stuff .prop1;
              modelAC.addItem(myVO);
              }

              You'd get the same item in every index. Sometimes this can be fairly
              subtle, such as if you're handing the object back and forth a lot and don't
              notice that you didn't make a new one at the top of the loop.

              HTH;

              Amy