collection.add() question!

For questions and postings not covered by the other forums
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

collection.add() question!

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:41 pm

by brokolyx >> Fri, 8 Oct 2004 14:09:51 GMT

I have a button called process and every time i click on it i assume that the a new refference of data will be added to an array. But infact it only does it once and only once, as i have checked the size of an array. I expect it to keep adding new referrences every time i push process. Here is my code.

create ary transient;
create sale;
sale.setPMovSalesPropertiesOnCreate(c, value, qty, currentDate, currentTime);
ary.add(sale);

the array size is still 1 no matter how many times add.

Thanks in advance

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: collection.add() question!

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:41 pm

by allistar >> Fri, 8 Oct 2004 18:32:21 GMT

Is it not because you are creating the array each time you click the button? So instead of reusing the same array each time, you create a new one - hence it will only have one item in it. There will be a lot of other transient arrays floating around on that process with one object in them as well.

I don't know what your goal is with this code, but you could hold the array as an exclusive collection against the form, so there is always and only one of them. The creation and deletion of that array is left up to JADE and all you need to do is add to it.

Regards,
Allistar.

--
------------------------------------------------------------------
Allistar Melville
Software Developer, Analyst allistar@silvermoon.co.nz
Auckland, NEW ZEALAND

Silvermoon Software
Specialising in JADE development and consulting
Visit us at: http://www.silvermoon.co.nz
*NEW* Simple web access to Jade at: www.silvermoon.co.nz/jhp.html ------------------------------------------------------------------

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: collection.add() question!

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:42 pm

by brokolyx >> Sat, 9 Oct 2004 5:54:09 GMT

I was wondering how do i create such exclusive arrray as you have mentioned.

The reason i am doing this is, because every time a new record is added to the i want to add that data into an array so that later i could sum the total outcome.

As everytime a record is create a new profit value for that day the records are inserted into jade database and i wish to collect all the databases using foreach loop with the help of array and sum the total price.

And i use this foreach loop to test it out:
countTotalSalesPerth();

vars
ary : BoxOfficeArray;
sale : MovieSales;
i : Integer;
begin
ary := BoxOfficeArray.firstInstance();
foreach sale in ary do
i := i + 1;
write i;
endforeach;
write "SALES ARRAY SIZE: " & ary.size.String;
write "SALES MAX SIZE: " & ary.maxSize.String;
end;

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: collection.add() question!

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:42 pm

by allistar >> Sat, 9 Oct 2004 7:34:31 GMT

A better way would be to have a root object (let's call it "BoxOfficeRoot" for now) and have only one instance of this. On this class you would define a collection called "allBoxOfficeSales" of type "BoxOfficeDict" (it would be better to use a dictionary as removing objects from arrays can be very slow. Also with a dictionary it would be keyed by something (like "date" for instance) so doing a query on it would be able to be optimised).

The MovieSale class would have an reference called "myBoxOfficeRoot" which would be inversed to "allBoxOfficeSales". This way you have database integrety (without an inverse if you deleted one of the MovieSale objects the collection would be left with a "hole" in it, which isn't good).

When you create a movie sale it would make sense to have some sort of "set" method that would take the details of the sale, such as:

date: Date; amount: Decimal etc.

If you want to see a total of all sales in the database, instead of summing them in a loop you could always sum them as the sale is added. Do this by adding a property called "totalMovieSalesValue" and in th "set" method on MovieSale you would do something like:

app.myBoxOfficeRoot.totalMovieSales := app.myBoxOfficeRoot.totalMovieSales + amount;

(although it would be better to put a "updateTotalMovieSales" method on BoxOfficeRoot).

you'll also need to make sure that when you delete a MovieSale then you reduce this total. Doing it this way means that you always have a running total and you don't need to accumulate it each time you want it.

Using ".firstInstance()" in production code is never a good idea, it pays to have a root object and hang things off that. Then you just need to go "app.myBoxOfficeRoot" to get the BoxOfficeRoot object. In the application initialise you'll need a "myBoxOfficeRoot := BoxOfficeRoot.firstInstance()" (which breaks my previous "rule", but in this case it is necessary).

(Also in your code you posted you could have achieved the whole things with this one line:

write "SALES ARRAY SIZE: " & BoxOfficeArray.firstInstance().size().String;

)

I hope this helps,
Allistar.
--
------------------------------------------------------------------
Allistar Melville
Software Developer, Analyst allistar@silvermoon.co.nz
Auckland, NEW ZEALAND

Silvermoon Software
Specialising in JADE development and consulting
Visit us at: http://www.silvermoon.co.nz
*NEW* Simple web access to Jade at: www.silvermoon.co.nz/jhp.html ------------------------------------------------------------------

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: collection.add() question!

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:42 pm

by brokolyx >> Sat, 9 Oct 2004 12:43:24 GMT

Thanks a lot Allistar ;) that has helped heaps


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest