Deep Copy of an ArrayList ?

M

Marc Lefebvre

there is an easy way to make a deep copy of an arraylist ?

suppose I have an arraylist of COptions implements ICloneable

I have need to overload the Clone() methode of a derived ArrayList
(OptionsArrayList)
And Overload the Clone() methode of my COptions

There is an otherway to do deep copy ?


Thank's
Marc
 
W

William Ryan eMVP

Hi Marc:

Here's Tom Shelton's answer from the VB Group:

Marc,

You could make sure the objects in it are marked serializable. Then you
can serialize the arraylist to a memrory stream, and then deserialize it.
You will then have a deep copy :)

<Serializable()> _
Class TheClass
' make sure all the members are serializable :)
....
End Class

dim arr as new arraylist

for i as integer = 1 to 100
arr.add(new theclass)
next

dim mem as new memroystream
dim bf as new binaryformatter

bf.serialize(mem, arr)
mem.seek(seekorigin.begin) ' put the pointer back or you get an error..
dim newarr as arraylist = directcast(bf.deserialize(mem), theclass)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top