473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to copy one collection to another?

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
Nov 20 '05 #1
6 17461
Jaro <sa*****@poczta.onet.pl> wrote in news:bjp28p$e92$1
@atlantis.news.tpi.pl:
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/de...l=/library/en-
us/cpref/html/frlrfsystemicloneableclassclonetopic.asp

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 20 '05 #2
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 Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. .
Jaro <sa*****@poczta.onet.pl> wrote in news:bjp28p$e92$1
@atlantis.news.tpi.pl:
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/de...l=/library/en-
us/cpref/html/frlrfsystemicloneableclassclonetopic.asp

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 20 '05 #3


Lucas Tam wrote:
Jaro <sa*****@poczta.onet.pl> wrote in news:bjp28p$e92$1
@atlantis.news.tpi.pl:

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/de...l=/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

Nov 20 '05 #4


Robert Jacobson wrote:
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

Nov 20 '05 #5
"Jaro" <sa*****@poczta.onet.pl> schrieb
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/de...l=/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?


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.
--
Armin

Nov 20 '05 #6
Cor
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
Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matthias S. | last post by:
Hi, I'm just wondering about the easiest way to copy certain values from a collection to a simple array. Say I've got a class (RaceCarDriver) with a DateFirstRace property. Now I'd like to...
3
by: Sakharam Phapale | last post by:
Hi All, eg. "Array.Copy" method used to copy array elements. Is there any method to copy collection objects data, except iterating through original collection and then filling each element...
11
by: DogEye | last post by:
In the C# , I want to change the object only after the user click the "Submit" button , so I first new an object and use the "=" to get the object in memory , I found that the operation "=" only...
4
by: JonZ | last post by:
I want to declare a local copy of an object so I can modify the local version without affecting the original copy. But when I update my local copy, the original object changes, too. My...
3
by: muchan | last post by:
I'm a C++ programmer now poking into C#. I wanted to write a snippet of code, equivalent of //--- C++ code --------------- #include <string> #include <vector> class obj { // a POD class...
5
by: Muskito | last post by:
Hi, Is it possible to copy the entire List(Of type) object to another List(Of type) object (they are both of the same type ofcourse) - without keeping the reference? There's the CopyTo...
8
by: downwitch | last post by:
Either I don't understand (entirely possible), or there's no way to copy parts of a class hierarchy from one instance to another. Say I have a class called Foo, and it contains, among other...
4
by: Kyote | last post by:
I'm trying to persist a list of filenames. I've made a custom collection and a FileName class: 'Class to hold file name information Public Class FileNames Public fullName As String Public...
19
by: Angus | last post by:
I have a socket class CTestClientSocket which I am using to simulate load testing. I create multiple instances of the client like this: for (int i = 0; i < 5; i++) { CTestClientSocket* pTemp...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.