collection clone?  
Author Message
cbl





PostPosted: Tue Jun 28 10:03:38 CDT 2005 Top

Visual Basic >> collection clone? is there an optimal way to clone a collection? i was going to do something
like:

...
Private m_Collection As Collection
...
Public Function Clone() As Collection

Set Clone = New Collection

Dim Item As Variant
For Each Item In m_Collection
Clone.Add Item
Next

End Sub

Problem is that I loose my Index. Is there a better way?

Thanks,

Craig Buchanan

Visual Studio18  
 
 
Ken





PostPosted: Tue Jun 28 10:03:38 CDT 2005 Top

Visual Basic >> collection clone? "Craig Buchanan" <msnews.microsoft.com> wrote in message
news:eA1k$C$EMail@HideDomain.com...
> is there an optimal way to clone a collection? i was going to do
> something
> like:

You can save the Index using....

Dim i As Integer
For i = 1 to m_Collection.Count
Clone.Add Item
Next

...but, you lose the Key. I guess it depends on which you use most. Looking
at your code, it seems that you don't use the Key so maybe it's good enough.

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..


 
 
Craig





PostPosted: Tue Jun 28 10:34:42 CDT 2005 Top

Visual Basic >> collection clone? Ken-

Thanks for the reply. I made a mistake. I do want to keep the key.
perhaps i need a mechanism to store the keys.

Do you have a recommendation?

Thanks,

Craig

"Ken Halter" <EMail@HideDomain.com> wrote in message
news:%23URbQK$EMail@HideDomain.com...
> "Craig Buchanan" <msnews.microsoft.com> wrote in message
> news:eA1k$C$EMail@HideDomain.com...
> > is there an optimal way to clone a collection? i was going to do
> > something
> > like:
>
> You can save the Index using....
>
> Dim i As Integer
> For i = 1 to m_Collection.Count
> Clone.Add Item
> Next
>
> ...but, you lose the Key. I guess it depends on which you use most.
Looking
> at your code, it seems that you don't use the Key so maybe it's good
enough.
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
> Please keep all discussions in the groups..
>
>


 
 
Phill





PostPosted: Tue Jun 28 10:46:16 CDT 2005 Top

Visual Basic >> collection clone? "Craig Buchanan" <msnews.microsoft.com> wrote in message
news:eA1k$C$EMail@HideDomain.com...
> is there an optimal way to clone a collection?

Not really. Even if you can iterate through the items, you'd have
to have a copy of each "key" somewhere in the held object,
because you can't retrieve the Key for a Collection item.

> i was going to do something like:
. . .
> For Each Item In m_Collection
> Clone.Add Item
> Next

Problem #2 - you wind up with a new /Collection/ object, but
it will be filled with references to all the /original/ items.

HTH,
Phill W.


 
 
Ken





PostPosted: Tue Jun 28 11:14:54 CDT 2005 Top

Visual Basic >> collection clone? "Craig Buchanan" <msnews.microsoft.com> wrote in message
news:%23Jrpob$EMail@HideDomain.com...
> Ken-
>
> Thanks for the reply. I made a mistake. I do want to keep the key.
> perhaps i need a mechanism to store the keys.
>
> Do you have a recommendation?
>
> Thanks,
>
> Craig

Well... you can get the keys (in an array) using this old hack....

Retrieve Key from Collection Item
http://groups.google.co.uk/group/microsoft.public.vb.general.discussion/msg/19c8d82c7ed7cc95?hl=en

...then, while For/Next looping, assign the keys to the new collection
items.

Phil mentioned a problem with references. If that collection contains
objects, the new collection will be pointing to those same objects and any
changes made to one of those objects will show up in both collections. If
that's a problem, it'll take more work.

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..