Bug in tCollection.ForEach? 
Author Message
 Bug in tCollection.ForEach?

>In one of my programs I use a collection of collections.
>When I wrote a routine to do some calculations on all items in
>this construction using the standard method FOREACH, my program
>didn't respond as I expected.

This isn't a bug, just a misuse of them.  The documentation isn't clear that
what you're doing is wrong, but it is:

>FUNCTION tSecondColl.calcTotal : integer;
>VAR subTotal : integer;
>  PROCEDURE addObject(obj : pMyObject); FAR;
>  BEGIN subTotal := subTotal + obj^.value END;
>  PROCEDURE addFirstColl(coll : pFirstColl); FAR;
>  VAR i : integer;

Here's the problem.  Your addObject procedure is a local procedure, but that's
not enough:  it has to be local to the routine that calls ForEach.  Change
this part of your code as shown and things should be fine:

FUNCTION tSecondColl.calcTotal : integer;
VAR subTotal : integer;

  PROCEDURE addFirstColl(coll : pFirstColl); FAR;
    PROCEDURE addObject(obj : pMyObject); FAR;
    BEGIN subTotal := subTotal + obj^.value END;

(I deleted the declaration of i here because you weren't using it; this really
has nothing to do with the problem.)

Duncan Murdoch

Wed, 18 Jun 1902 08:00:00 GMT  
 [ 1 post ] 

 Relevant Pages 

1. FPC: Wrong behavior in nested use of TCollection.ForEach

2. Temporary Tables / FOREACH

3. ForEach and traversal methods in OOP data structures

4. HELP! ForEach/FirstThat methods

5. HELP! ForEach/FirstThat methods

6. ForEach and traversal methods in OOP data structures

7. How to make TCollection that sorts on demand?

8. What are TCollection's accessor methods?

9. Problem with TCollection and DLLs

10. TCollection and XMS

11. ??C++ analog of TCollection ??

12. Tcollection into Clipboard


Powered by phpBB® Forum Software