473,542 Members | 2,045 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I make a copy of my collection?

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
End Class
Now I create a new object and fill the above strings then add it to my
custom collection. So far this all works just fine.

Now, once I've iterated through my collection and performed the
actions on each item I wish to perform it's done. But I got tired of
reloading the same list each time I decided to work on the same files,
so I thought to copy the collection before I made my changes. I tried

myBackupQueue = myQueue

and that seemed to work, until I went to reload the collection anyway.
What I'm doing is emptying the original list right before I try to do

myQueue = myBackupQueue

But this here seems to be my problem. I could be wrong but it seems
that when I originally make my copy of the collection with

myBackupQueue = myQueue

thats it's only putting a reference to each object contained in the
original myQueue because when I empty it out like:

myQueue.Clear()

both myQueue and myBackupQueue are emptied. Maybe I'm just tired, but
I can't think of a way around this.

Can anyone see what's happening and what I can do to accomplish what
I'm trying to do? Any constructive help would be HIGHLY appreciated.
--
Kyote
Sep 10 '06 #1
4 2760
Kyote,
First, collections are reference types. When you make the assignment
myBackupQueue = myQueue you are only copying the reference. This is the
expected behavior for reference types. To make a true copy you have to
create a new instance and populate the data.

You seem to be collecting file system data, you should look into the
FileInfo/Path classes in the System.IO namespace

The easiest way I know of to perform the clone (backup) is to mark the class
as Serializable and perform a simple serialization/deserialization to make a
clone.

Attached is a sample console application to get you moving in the right
direction.

Jared

Imports System
Imports System.Collecti ons
Imports System.Collecti ons.Specialized
Imports System.IO

Module Module1

Sub Main()
Dim myQueue As New CustomFileQueue ()
Dim myBackupQueue As CustomFileQueue = Nothing
myQueue.Add(New FileNames("boot .ini", "boot", "ini", "c:\boot.in i",
"bootBackup "))
myQueue.Add(New FileNames("test .txt", "test", "txt",
"c:\temp\test.t xt", "testing"))
myBackupQueue = myQueue.Clone()
myQueue.Clear()
Console.WriteLi ne(myBackupQueu e.Count)
End Sub

End Module
<Serializable() _
Public Class CustomFileQueue : Inherits CollectionBase : Implements ICloneable

Public Function Add(ByVal FileNamesInstan ce As FileNames) As Integer
Return MyBase.List.Add (FileNamesInsta nce)
End Function

Public Function Clone() As Object Implements System.ICloneab le.Clone
Dim formatter As
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er = _
New
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er()
Dim mem As New System.IO.Memor yStream()
Try
formatter.Seria lize(mem, Me)
mem.Flush()
mem.Seek(0, IO.SeekOrigin.B egin)
Return formatter.Deser ialize(mem)
Finally
If Not mem Is Nothing Then
mem.Close()
mem.Dispose()
End If
End Try

End Function

End Class

<Serializable() _
Public Class FileNames

Public Sub New(ByVal fullName As String, ByVal fileName As String, ByVal
fileExtension As String, ByVal filePath As String, ByVal newName As String)
Me.fullName = fullName
Me.fileName = fileName
Me.fileExtentio n = fileExtension
Me.filePath = filePath
Me.newName = newName
End Sub

Public fullName As String
Public fileName As String
Public fileExtention As String
Public filePath As String
Public newName As String

End Class

"Kyote" wrote:
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
End Class
Now I create a new object and fill the above strings then add it to my
custom collection. So far this all works just fine.

Now, once I've iterated through my collection and performed the
actions on each item I wish to perform it's done. But I got tired of
reloading the same list each time I decided to work on the same files,
so I thought to copy the collection before I made my changes. I tried

myBackupQueue = myQueue

and that seemed to work, until I went to reload the collection anyway.
What I'm doing is emptying the original list right before I try to do

myQueue = myBackupQueue

But this here seems to be my problem. I could be wrong but it seems
that when I originally make my copy of the collection with

myBackupQueue = myQueue

thats it's only putting a reference to each object contained in the
original myQueue because when I empty it out like:

myQueue.Clear()

both myQueue and myBackupQueue are emptied. Maybe I'm just tired, but
I can't think of a way around this.

Can anyone see what's happening and what I can do to accomplish what
I'm trying to do? Any constructive help would be HIGHLY appreciated.
--
Kyote
Sep 10 '06 #2
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
End Class
What is the purpose of the newName string? I ask this because you seem
to be re-inventing the wheel. What version of VS are you using? Why not
just use a generic List and the FileInfo class that is part of the
System.IO namespace?

Sep 11 '06 #3
On Sun, 10 Sep 2006 05:23:01 -0700, Jared
<Ja***@discussi ons.microsoft.c omwrote:
>Kyote,
First, collections are reference types. When you make the assignment
myBackupQueu e = myQueue you are only copying the reference. This is the
expected behavior for reference types. To make a true copy you have to
create a new instance and populate the data.

You seem to be collecting file system data, you should look into the
FileInfo/Path classes in the System.IO namespace

The easiest way I know of to perform the clone (backup) is to mark the class
as Serializable and perform a simple serialization/deserialization to make a
clone.

Attached is a sample console application to get you moving in the right
direction.

Jared
Thank you Jared. I appreciate you taking the time to help me. Sorry
for it taking me so long to reply.

Serializable is something I need to read up on. I'll play around with
what you've suggested and let you know how it goes. Thanks again for
the help.

---
Kyote
Sep 17 '06 #4
On 11 Sep 2006 06:06:56 -0700, "Chris Dunaway" <du******@gmail .com>
wrote:
>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
End Class

What is the purpose of the newName string? I ask this because you seem
to be re-inventing the wheel. What version of VS are you using? Why not
just use a generic List and the FileInfo class that is part of the
System.IO namespace?
Hello Chris.

Well, I'm using VS.net 2005.

I love to read. I sometimes get books from binary newsgroups and it's
gets tedious trying to organize my files in a format I can quickly and
easily scan through to find something of interest to me. So I thought
this was a perfect example of when I could put my limited programming
skills to use and to also further my knowledge of the VB.net language.

I have read about the FileInfo class and may end up using it more than
I currently do. The newName string will require a bit of an
explanation of what I'm trying to do.

My ebook files number over 15,000, currently on my hard drive. I have
a lot more stored on a dvd and I plan on organizing all them once I
get a few simple apps to help me with the effort.

This app is simply to help me try to conform some of the files names
to what a later app will then organize. Some filenames have multiple
consecutive spaces. Some have odd characters. Some are simply
misspelled. And some have strange, or odd name arrangement. Well,
here's some examples.

1) (ebook) - Pratchett, Terry - Discworld - 02 - The Light
Fantastic.txt
2) Terry_Pratchett _-_Discworld_- 02_-_The_Light_Fant astic.txt
3) Terry Pratchett - Discworld - 02 - The Light Fantastic.txt
4) Tery Pratchett - Discworld - 02 - The Light Fantastic.txt
5) Tarry Pratchett - Discworld - 02 - The Light Fantastic.txt
6) Pratchett, Terry - Discworld - 02 - The Light Fantastic.txt

To me 1-5 are wrong. At least by my current preference. In 1 through 5
I want my app to let me change all files to be similar to item 6. So
my app will let me choose the directories to load then I can scan
through my list of added filenames and find ones with something I want
or need to change. Then in a textbox I type in what I see that I want
either removed, rearranged, or replaced. Like replacing all
underscores with spaces and removing all consecutive spaces.

My next app will create folders for each author, like this:

Pratchett, Terry
pdf
htm
txt
Eddings, David
pdb
txt

And then even sub folders for file types. In fact as I go along I keep
thinking of new ideas and enhancements to what I currently want to do.
Like now I want the choice of Author Names or Extention to be
selectable. Having a folder with all txt ebooks and sub folders for
all authors with their respective books in their self named directory
would be really nice. Like:

TXT
Eddings, David
Pratchett, Terry

But all of this is also getting me more experience with VB.net and VS.
Sometimes it seems so overwhelming! At those times I'm very thankful
that I learned of newsgroups. In my opinion, discussion newsgroups are
one of the most valuable resources the net has to offer.

Now, the reason I do it this way is because I figured calling the
fileinfo (file system) as many times as could be needed would be too
costly. In fact, I now see that I don't even need to make a copy of my
original Queue. I can just load the changed name into the listbox and
allow it to continue to be updated until I'm done making all my
adjustments. Then it'll iterate through the whole list changing the
names as needed.

If you, or anyone, have any suggestions I'd be happy to hear them. I
know in programming there are usually too many different ways to go
about doing the same thing. So any advice is highly appreciated.
---
Kyote
Sep 17 '06 #5

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

Similar topics

2
1444
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 pass all DateFirstRace-Values in my RaceCarDriverCollection to some function as a DateTime. Any help is greatly appreceated.
3
20631
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 into other collection, which is a lengthy process. colBackupEmp = m_colEmp Above statement sets the reference.
6
17481
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
4
1370
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 original object is a collection I’ve defined. I started with a class definition: Public Class MyClassDef Inherits System.Collections.CollectionBase
5
25631
by: BenW | last post by:
Hello, What is the easiest way to make "deep copy" of my Hashtable? How about with other Collection classes in C#, any documents available? I don'r actually understand why Framework's Collection classes does not support deep copy methods by theself, only Clone with shallow copy.
5
18983
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
9620
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...
2
2720
by: Lubomir | last post by:
Hi, I would like to ask if the constructor ArrayList(ICollection c) performs the deep or shallow copy of "c" collection. If it perfoms just shallow copy, is there any method for doing a deep one? Thanks, Lubomir
19
4170
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);
0
7392
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
7571
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. ...
1
7324
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7670
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
5867
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...
1
5246
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...
0
3376
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
943
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
620
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.