473,566 Members | 2,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to clone a collection?

phx
I have to collection that stores the items from the same class. I want to
copy the items of the second collection to the first one , and empty the
second.
first.clean()
first=second
second()

causes an error in the program , it seems when i clear the second one , alse
referances in the first one also empties. Anyway to clone these?
thank you
Nov 21 '05 #1
2 8133
Public Class MyCollection
Inherits CollectionBase
Implements ICloneable

#Region " ICloneable Implementation "

Private Function IClone() As Object Implements ICloneable.Clon e
'Implemented in Public Function Clone()
End Function

Public Function Clone() As MyCollection

'---------------------------------------------------------------------------
------
'Create a copy of the collection

'---------------------------------------------------------------------------
------
Dim collection As New MyCollection()
'Loop through each item and add it to the copy
Dim item As MyObject
For Each item In Me.InnerList
'Use InnerList to prevent raising events ?????
collection.Inne rList.Add(eProj ect)
Next
'Return the copy
Return collection
End Function

#End Region

End Class
'------------------------------------------------------------------
'Clone the second collection
MyFirstCollecti on = MySecondCollect ion.Clone
'Clear the second collection
MySecondCollect ion.Clear

Stephen

"phx" <hd****@yahoo.c om> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
I have to collection that stores the items from the same class. I want to copy the items of the second collection to the first one , and empty the
second.
first.clean()
first=second
second()

causes an error in the program , it seems when i clear the second one , alse referances in the first one also empties. Anyway to clone these?
thank you

Nov 21 '05 #2
phx
thank you for your answer

i guess "collection.Inn erList.Add(ePro ject)" command is wrong, i changed the
eproject with "item" . but made me wonder where "eproject" came from.

"Stephen Muecke" <st*****@senet. com.au> wrote in message
news:Ol******** ******@TK2MSFTN GP11.phx.gbl...
Public Class MyCollection
Inherits CollectionBase
Implements ICloneable

#Region " ICloneable Implementation "

Private Function IClone() As Object Implements ICloneable.Clon e
'Implemented in Public Function Clone()
End Function

Public Function Clone() As MyCollection

'---------------------------------------------------------------------------
------
'Create a copy of the collection

'---------------------------------------------------------------------------
------
Dim collection As New MyCollection()
'Loop through each item and add it to the copy
Dim item As MyObject
For Each item In Me.InnerList
'Use InnerList to prevent raising events ?????
collection.Inne rList.Add(eProj ect)
Next
'Return the copy
Return collection
End Function

#End Region

End Class
'------------------------------------------------------------------
'Clone the second collection
MyFirstCollecti on = MySecondCollect ion.Clone
'Clear the second collection
MySecondCollect ion.Clear

Stephen

"phx" <hd****@yahoo.c om> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
I have to collection that stores the items from the same class. I want

to
copy the items of the second collection to the first one , and empty the
second.
first.clean()
first=second
second()

causes an error in the program , it seems when i clear the second one ,

alse
referances in the first one also empties. Anyway to clone these?
thank you


Nov 21 '05 #3

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

Similar topics

0
2656
by: Jason Evans | last post by:
Hi All, I am writing my own implementation of queue via a linked list, note not a LinkedList, and was running into trouble with the clone method. I was wondering if anyone could point out some not so obvious errors I have made.. Cheers
1
1298
by: Jeff Haumesser | last post by:
I have a Collection called Grooming. Each object in this collection contains the property GroomingServices of type Collection. I also have a Clone of this Groomings collection called GroomingsInit. The sub-collection GroomingServices has also been cloned. My question is: Can I simply set Groomings = GroomingsInit? Will the sub-collection...
8
3818
by: Noozer | last post by:
I'm looking for a way to generate a "clone" of an object. Right now I need to write a Clone function for every class that make and I'd like to have a generic routine. Instead of doing this: For i = 0 to Me.Count-1 Set obj = New cSkill obj.ID = Me.Item(i).ID obj.Desc = Me.Item(i).Desc obj.... etc.
2
11453
by: Steven | last post by:
Hi, I have created my own node (class MyNode : TreeNode) for a TreeView. To populate the treeview, i use something like MyNode newNode = new MyNode("Bla bla bla","0","1") for example. But, to move the nodes in the treeview, i use the clone() method... : MyNode theCopy = (MyNode)theTreeView.SelectedNode.Clone();
1
4397
by: Neeraj | last post by:
hi I want to make clone of current Form which is currently seen.I Used attribute above the class declaration which extends Form. Impleament the ICloneable interface and Make method which code is public Object Clone() { return this.MemberwiseClone(); } public static object DeepClone(
16
2492
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some of properties or assign event handlers. I need to be able to clone the manipulated control at runtime. I could use the Clone method of some...
14
8586
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event handlers are set like the old one. Is there a way to do this job? For example, is there a way to use EventInfo object to get all event handlers of...
7
2815
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds to support cloning: Public Class RefClass Public tcp As TcpClient Public name As String End Class Public Class RefClassList Inherits List(Of...
14
3892
by: Ste | last post by:
Good morning, what can be the best way to clone an object with events ? This because I have made a deep copy of my object and added new event handlers but events are fired from the original instance, not in cloned instance. Thank you. Stefano.
0
7673
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...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7893
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. ...
0
8109
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...
0
7953
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6263
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...
0
3643
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...
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.