468,536 Members | 1,540 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,536 developers. It's quick & easy.

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 2560
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.Collections
Imports System.Collections.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.ini",
"bootBackup"))
myQueue.Add(New FileNames("test.txt", "test", "txt",
"c:\temp\test.txt", "testing"))
myBackupQueue = myQueue.Clone()
myQueue.Clear()
Console.WriteLine(myBackupQueue.Count)
End Sub

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

Public Function Add(ByVal FileNamesInstance As FileNames) As Integer
Return MyBase.List.Add(FileNamesInstance)
End Function

Public Function Clone() As Object Implements System.ICloneable.Clone
Dim formatter As
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter = _
New
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter()
Dim mem As New System.IO.MemoryStream()
Try
formatter.Serialize(mem, Me)
mem.Flush()
mem.Seek(0, IO.SeekOrigin.Begin)
Return formatter.Deserialize(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.fileExtention = 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***@discussions.microsoft.comwrote:
>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
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_Fantastic.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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Sakharam Phapale | last post: by
5 posts views Thread by BenW | last post: by
2 posts views Thread by Lubomir | last post: by
19 posts views Thread by Angus | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.