473,785 Members | 2,841 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 BackupEmployeeD ata()
''''''' 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 20643
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.co m

"Sakharam Phapale" <sp******@annet site.com> wrote in message
news:e6******** ******@tk2msftn gp13.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 BackupEmployeeD ata()
''''''' 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******@annet site.com> wrote in message
news:e6******** ******@tk2msftn gp13.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 BackupEmployeeD ata()
''''''' 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
15180
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
17551
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 references to d2. how to implement correct copy function? regards jaro
3
2451
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 public: std::string s;
6
4902
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 the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
5
19061
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 method, but it only copies the data to a flat array. I Need this for Backup in my program, when i update the master list and
8
9632
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 things, a custom collection called SubFoos. If I do something like Dim MyFoo1 As Foo Dim MyFoo2 As Foo 'call something to populate MyFoo1 and its SubFoos...
4
2774
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 fileName As String Public fileExtention As String Public filePath As String Public newName As String
19
4209
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 = new CTestClientSocket(this, ip, port); pTemp->Connect(); m_collClients.push_back(pTemp);
82
3739
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. Let's name the update method as doUpdate and stl::map read methods as getData and copyData. Since stl::map is not thread-safe, we should do synchronization by ourselves. A usable solution is to create a boost::mutex::scoped_lock object in all above...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10330
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10153
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8976
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.