Board index » delphi » Copy Collection...

Copy Collection...

Quote
In article <3168DCC5.1...@ms2.hinet.net> Jason wrote:

>>Could anyone tell me: 'How to copy a collection'?

>>My program will run on a very simple PC (without HD and EMS),
>>so I can't use any kind of streams.
>>My problem is: I must pass a collection to an object to
>>edit it. If the user press 'Cancel', the collection must
>>be restored by it's original value. So I guess I gota make
>>a copy of the collection. but...
>>... How to make a copy of a collection ???

> I've written a small piece of code to 'Copy a collection':

[code sample]

Quote
> The program hanged after the line 'PColl1^.FreeAll' was executed.

(why?)

Jason, you're walking on thin ice and not thinking too clearly about it.  You must have been told that a stream would solve your problem.  Evidently you don't understand why.  Remember that a collection handles pointers.
 So when you "copy" an object from one collection to another you simply replicated the pointer.  This simply gives you two pointers to the _same_ object.  When the temporary collection freeall free's the one=and=only inst
ance of the object.

Obviously this is not what you want. Especially since the edited objects are to revert back to their original state if the user cancels the operation.  You not only have to create a new collection, but you have to create
a new instance of every object in the collection.  This would be relatively easy to do if the collection contained strings or some type of record.  Objects however are a different story.

How do you create a specific instance of an object when you have no
idea of the type of object or its contents?  I simplest solution I
can think of accomplishing this is by using a stream.  If you can't
use a DOS stream and can't use an EMS stream, can you use a memory
stream?

The only other solution I can think of is to add a method to each
object that will "clone" itself.  But that's what a stream does.  
In your ForEach process, why not reset a memory stream, have the
collection stream the object using PutItem then reset the stream
again and have the new collection retrieve the object using
GetItem.  I'm suggesting to use the collection's PutItem and
GetItem so that the mechanism will work even if the collection does
not contain objects.  It shouldn't take too much memory if you
replicate one entry at a time.

 

Re:Copy Collection...


Quote
>Could anyone tell me: 'How to copy a collection'?

>My program will run on a very simple PC (without HD and EMS),
>so I can't use any kind of streams.
>My problem is: I must pass a collection to an object to
>edit it. If the user press 'Cancel', the collection must
>be restored by it's original value. So I guess I gota make
>a copy of the collection. but...
>... How to make a copy of a collection ???

 I've written a small piece of code to 'Copy a collection':

   procedure TMyDialog.Edit;
       procedure CopyCollect(P:PIOItem); far;
           begin
           PColl1^.insert(P);
           end;
       procedure RestoreCollect(P:PIOItem); far;
           begin
           PColl^.insert(P);
           end;
       begin
       PColl^.forEach(@CopyCollect);     { Copy PColl to PColl1}
         :
       PColl1^.FreeAll;
       end;

 where PColl and PColl1 are both members of TMyDialog
 (Type PCollection), There are two objects in the collection PColl,
 both are of type PIOItem:

  PIOItem = ^TIOItem;
  TIOItem = object(TObject)
     ID     : byte;
     name   : string[20];
     Delay  : word;
     mode   : byte;
     constructor init(ID1:byte; Name1:String; Delay1:word; mode1:byte);
     end;

 The program hanged after the line 'PColl1^.FreeAll' was executed. (why?)

Re:Copy Collection...


Quote
Jason (jason...@ms2.hinet.net) wrote:

: Hi R. E. Donais,

: You are right! I just forgot that the iterator procedure doesn't
: pass an instance but a POINTER! I think the only solution is stream
: or any idea like stream. TP6 doesn't provide memory streams. I don't
: know if I can find one on the web. Could you passibly provide one?
: Anyway, I'll use TBufStream at this moment. If I can't find any
: memory stream before my program released, I'll try to design my own.

: Thanks for your help.

: Jason

Try getting Duncan Murdoch's  STREAMS16.ZIP from Garbo or wherever. It
contains invaluable stream types and tricks, including a RAM stream
(limited to 64K however), an XMS stream, concatenated streams,
temporary streams, and so on and on and on...

Eyal Doron

Other Threads