473,495 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
Create 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 2754
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1440
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...
3
20626
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...
6
17475
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...
4
1366
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...
5
25626
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...
5
18969
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
9610
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...
2
2715
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...
19
4166
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...
0
7120
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
6991
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
7160
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,...
0
7196
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
7373
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
5456
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,...
0
4583
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
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...
1
649
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.