how to copy one collection to another?

  • Thread starter Thread starter Jaro
  • Start date Start date
J

Jaro

hi there,
I've problem to create copy of collection of classes.
ex.: d1 as mycollection, d2 as mycollection.
when I simple set d1=d2 then d1 contain all classes from d2 but their
properties are references to d2.
how to implement correct copy function?

regards
jaro
 
hi there,
I've problem to create copy of collection of classes.
ex.: d1 as mycollection, d2 as mycollection.
when I simple set d1=d2 then d1 contain all classes from d2 but their
properties are references to d2.
how to implement correct copy function?

I believe you need to implement the ICloneable interface (the clone
function) if you want to do a deep-copy. If only a shallow copy is
required, you can try using MemberwiseClone.


http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpref/html/frlrfsystemicloneableclassclonetopic.asp
 
A quick-and-dirty alternative is simply to enumerate through the first
collection, and add each item in the first collection to the second
collection. E.g.,

For each X in d1
d2.add(X)
Next X

This would create a shallow copy of the the items in d1.
 
Lucas said:
@atlantis.news.tpi.pl:




I believe you need to implement the ICloneable interface (the clone
function) if you want to do a deep-copy. If only a shallow copy is
required, you can try using MemberwiseClone.


http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpref/html/frlrfsystemicloneableclassclonetopic.asp

I did it, but if use in function 'return me' I've still have references...
I'm totally newbie in .net :(
have you a little piece od code how to do it?

jaro
 
Robert said:
A quick-and-dirty alternative is simply to enumerate through the first
collection, and add each item in the first collection to the second
collection. E.g.,

For each X in d1
d2.add(X)
Next X

This would create a shallow copy of the the items in d1.

it sounds like code from vb6.
it _must_ be simplier way to do it in .net.

jaro
 
Jaro said:
I did it, but if use in function 'return me' I've still have
references... I'm totally newbie in .net :(
have you a little piece od code how to do it?

You must not write "return me", you have to return the object created in the
Clone method that implements ICloneable.Clone. Here is an example:

Public Class Class1
Implements ICloneable

Public Value1 As Integer
Public Value2 As Class2

Public Function Clone() As Object Implements System.ICloneable.Clone
Dim Result As Class1
Result = New Class1
Result.Value1 = Me.Value1
Result.Value2 = DirectCast(Me.Value2.Clone, Class2)
Return Result
End Function
End Class

Public Class Class2
Implements ICloneable
Public Value1 As Byte

Public Function Clone() As Object Implements System.ICloneable.Clone
Dim Result As Class2
Result = New Class2
Result.Value1 = Me.Value1
Return Result
End Function
End Class


This example also shows how to handle reference types. A class is a
reference type whereas an Integer and Byte is a value type. You can simply
copy a reference type by assigning the variable to another variable (see
Result.Value1 = Me.Value1). To copy an object that is a refernce type, you
must also call it's clone method because a simple assignment (like
result.value2 = me.value2) would only copy the reference to the same object
but not the object itself.
 
Jaro,
I've problem to create copy of collection of classes.
ex.: d1 as mycollection, d2 as mycollection.
when I simple set d1=d2 then d1 contain all classes from d2 but their
properties are references to d2.
how to implement correct copy function?
What is wrong with that
When I copy a geographical map the references on the copy of the map will be
to the same place.
I don't see what is logical wrong?
Cor
 
Back
Top