473,398 Members | 2,188 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,398 software developers and data experts.

How to make copy of collection object?

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
into other collection, which is a lengthy process.

colBackupEmp = m_colEmp
Above statement sets the reference.
---------------------------------------------------------------------

Structure Employee
Dim ID As Integer
Dim Name As String
End Structure

Private m_colEmp As New Collection
Private m_colBackupEmp As New Collection

Private Sub LoadEmployee()
Dim Emp as Employee
Emp.ID = 100
Emp.Name = "John"
m_colEmp.Add (Emp)

Emp.ID = 200
Emp.Name = "Sam"
m_colEmp.Add (Emp)
End Sub
Private Sub BackupEmployeeData()
''''''' colBackupEmp = m_colEmp ''''''''
Dim Emp As Employee
For Each Emp In m_colEmp
m_colBackupEmp.Add (Emp)
Next
End Sub

---------------------------------------------------------------------

Thanks and Regards
Sakharam Phapale
Nov 16 '05 #1
3 20623
Sakharam,

There really isn't, since the collection wouldn't know exactly what
needs to be copied. Most of the time, collections are of reference types
(just an observation) which don't have natural copy semantics. It's because
of this that I assume there is nothing native in the collection to copy it
to another collection of the same type.

You will have to implement this yourself.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Sakharam Phapale" <sp******@annetsite.com> wrote in message
news:e6**************@tk2msftngp13.phx.gbl...
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
into other collection, which is a lengthy process.

colBackupEmp = m_colEmp
Above statement sets the reference.
---------------------------------------------------------------------

Structure Employee
Dim ID As Integer
Dim Name As String
End Structure

Private m_colEmp As New Collection
Private m_colBackupEmp As New Collection

Private Sub LoadEmployee()
Dim Emp as Employee
Emp.ID = 100
Emp.Name = "John"
m_colEmp.Add (Emp)

Emp.ID = 200
Emp.Name = "Sam"
m_colEmp.Add (Emp)
End Sub
Private Sub BackupEmployeeData()
''''''' colBackupEmp = m_colEmp ''''''''
Dim Emp As Employee
For Each Emp In m_colEmp
m_colBackupEmp.Add (Emp)
Next
End Sub

---------------------------------------------------------------------

Thanks and Regards
Sakharam Phapale

Nov 16 '05 #2
Hi,

Assuming that you mean classes derived from CollectionBase the answer is
not, this does not prevent you of creating a method that do that, though.

A final remark, this is a C# group, the code you provided is VB.net

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Sakharam Phapale" <sp******@annetsite.com> wrote in message
news:e6**************@tk2msftngp13.phx.gbl...
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
into other collection, which is a lengthy process.

colBackupEmp = m_colEmp
Above statement sets the reference.
---------------------------------------------------------------------

Structure Employee
Dim ID As Integer
Dim Name As String
End Structure

Private m_colEmp As New Collection
Private m_colBackupEmp As New Collection

Private Sub LoadEmployee()
Dim Emp as Employee
Emp.ID = 100
Emp.Name = "John"
m_colEmp.Add (Emp)

Emp.ID = 200
Emp.Name = "Sam"
m_colEmp.Add (Emp)
End Sub
Private Sub BackupEmployeeData()
''''''' colBackupEmp = m_colEmp ''''''''
Dim Emp As Employee
For Each Emp In m_colEmp
m_colBackupEmp.Add (Emp)
Next
End Sub

---------------------------------------------------------------------

Thanks and Regards
Sakharam Phapale

Nov 16 '05 #3
Consider using a serializable class instead of a VB Collection object
(which is not serializable). Have that class implement ICloneable and
implement the Clone method of that interface. In the Clone method you
will need to use a BinaryFormatter.Serialize to serialize your class
(Me) into a MemoryStream. You would then return a
BinaryFormatter.DeSerialize object from the Clone method.
This will give you a "deep" copy of your class, which is what I think
you are striving for. My suggestion involves a little more coding but
if your are planning to work on a very large collection I believe the
processing time will not be as "lengthy" as using a For Each iteration.
I don't have any timings I can use to prove it, this is just a guess.
HTH,
JW

Nov 16 '05 #4

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

Similar topics

3
by: Jules | last post by:
I have a cutom object ex.: public class Client { public string Name; public string Id; public Adresses Adresses; public Client() { }
6
by: Jaro | last post by:
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...
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...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
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...
82
by: Bill David | last post by:
SUBJECT: How to make this program more efficient? In my program, a thread will check update from server periodically and generate a stl::map for other part of this program to read data from....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.