473,326 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Related collections of objects

I’m in need of some remedial instruction collections of objects. I doubt if
this question is specific to pocket pc and VB, but that’s where I’m coding.

When I create a collection, call it collection A, and then create a new
collection B, if I make a change to one of the items in collection B, the
exact same changes effect collection A. While I could see this as being a
feature, for what I’m doing… well, I wish it wouldn’t. Is there some simple
way that I can “sever” the linkage between these two collections?
Here’s an example of the problem

Dim Vegetables As New Collection
Dim NewVegetables As New Collection

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoadVeggies()
NewVegetables = Vegetables
Dim Vegetable0 As New Vegetable
For Each Vegetable0 In NewVegetables
If Vegetable0.Name = "Squash" Then
Vegetable0.Color = "Green"
End If
Next
End Sub
Private Sub LoadVeggies()
Dim vegetableA As New Vegetable
vegetableA.Name = "Carrot"
vegetableA.Color = "Orange"
Vegetables.Add(vegetableA)

Dim vegetableB As New Vegetable
vegetableB.Name = "Squash"
vegetableB.Color = "Yellow"
Vegetables.Add(vegetableB)

End Sub
End Class
In this code, when I set a brake pt at the end sub for the Form 1 load, I’d
like to see the If logic create only a green Squash in NewVegetables. The
Yellow Squash should stay in Vegetables. It doesn’t, I get a green squash in
both collections. Some how the two collections are linked and I need to
sever the link. Suggestions?
Nov 21 '05 #1
6 1234
Your problem is that you set NewVegetables equal to Vegetables. Because
they are not 'simple' types (like a string or integer), a reference is
made to the original rather than a copy being made. Here is an
optimization of your code with the fix:

Dim Vegetables As New Collection
Dim NewVegetables As New Collection

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoadVeggies()
For Each Veg as Vegetable In Vegetables
NewVegetables.Add(Veg)
Next
Vegetables.Clear
For Each Vegetable0 As Vegetable In NewVegetables
If Vegetable0.Name = "Squash" Then
Vegetable0.Color = "Green"
End If
Next
End Sub
Private Sub LoadVeggies()
Dim vegetableA As New Vegetable
vegetableA.Name = "Carrot"
vegetableA.Color = "Orange"
Vegetables.Add(vegetableA)

Dim vegetableB As New Vegetable
vegetableB.Name = "Squash"
vegetableB.Color = "Yellow"
Vegetables.Add(vegetableB)

End Sub
End Class

Dave J wrote:
I’m in need of some remedial instruction collections of objects.I doubt if
this question is specific to pocket pc and VB, but that’s where I’m coding.

When I create a collection, call it collection A, and then create a new
collection B, if I make a change to one of the items in collection B, the
exact same changes effect collection A. While I could see this as beinga
feature, for what I’m doing… well, I wish it wouldn’t. Is there some simple
way that I can “sever” the linkage between these two collections?
Here’s an example of the problem

Dim Vegetables As New Collection
Dim NewVegetables As New Collection

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoadVeggies()
NewVegetables = Vegetables
Dim Vegetable0 As New Vegetable
For Each Vegetable0 In NewVegetables
If Vegetable0.Name = "Squash" Then
Vegetable0.Color = "Green"
End If
Next
End Sub
Private Sub LoadVeggies()
Dim vegetableA As New Vegetable
vegetableA.Name = "Carrot"
vegetableA.Color = "Orange"
Vegetables.Add(vegetableA)

Dim vegetableB As New Vegetable
vegetableB.Name = "Squash"
vegetableB.Color = "Yellow"
Vegetables.Add(vegetableB)

End Sub
End Class
In this code, when I set a brake pt at the end sub for the Form 1 load, I’d
like to see the If logic create only a green Squash in NewVegetables. The
Yellow Squash should stay in Vegetables. It doesn’t, I get a green squash in
both collections. Some how the two collections are linked and I need to
sever the link. Suggestions?

Nov 21 '05 #2
Matt thanks, but I tried your code sample, and still got a bad result.
Collection "vegtables" was identical to collection "newvegtables".... both
contained "green squash" when only "newvegtables" should have.

I tried this where i started, as a pocketpc ap, and then again as a windows
ap.

something that might be telling: "Vegtables.clear" as a command was
rejected on my system as a valid command. perhaps we have different
environments?

hmmm.... any guidance would be appreciated
"Matt" wrote:
Your problem is that you set NewVegetables equal to Vegetables. Because
they are not 'simple' types (like a string or integer), a reference is
made to the original rather than a copy being made. Here is an
optimization of your code with the fix:

Dim Vegetables As New Collection
Dim NewVegetables As New Collection

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoadVeggies()
For Each Veg as Vegetable In Vegetables
NewVegetables.Add(Veg)
Next
Vegetables.Clear
For Each Vegetable0 As Vegetable In NewVegetables
If Vegetable0.Name = "Squash" Then
Vegetable0.Color = "Green"
End If
Next
End Sub
Private Sub LoadVeggies()
Dim vegetableA As New Vegetable
vegetableA.Name = "Carrot"
vegetableA.Color = "Orange"
Vegetables.Add(vegetableA)

Dim vegetableB As New Vegetable
vegetableB.Name = "Squash"
vegetableB.Color = "Yellow"
Vegetables.Add(vegetableB)

End Sub
End Class

Dave J wrote:
I’m in need of some remedial instruction collections of objects. I doubt if
this question is specific to pocket pc and VB, but that’s where I’m coding.

When I create a collection, call it collection A, and then create a new
collection B, if I make a change to one of the items in collection B, the
exact same changes effect collection A. While I could see this as being a
feature, for what I’m doing… well, I wish it wouldn’t. Is there some simple
way that I can “sever” the linkage between these two collections?
Here’s an example of the problem

Dim Vegetables As New Collection
Dim NewVegetables As New Collection

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoadVeggies()
NewVegetables = Vegetables
Dim Vegetable0 As New Vegetable
For Each Vegetable0 In NewVegetables
If Vegetable0.Name = "Squash" Then
Vegetable0.Color = "Green"
End If
Next
End Sub
Private Sub LoadVeggies()
Dim vegetableA As New Vegetable
vegetableA.Name = "Carrot"
vegetableA.Color = "Orange"
Vegetables.Add(vegetableA)

Dim vegetableB As New Vegetable
vegetableB.Name = "Squash"
vegetableB.Color = "Yellow"
Vegetables.Add(vegetableB)

End Sub
End Class
In this code, when I set a brake pt at the end sub for the Form 1 load, I’d
like to see the If logic create only a green Squash in NewVegetables. The
Yellow Squash should stay in Vegetables. It doesn’t, I get a green squash in
both collections. Some how the two collections are linked and I need to
sever the link. Suggestions?

Nov 21 '05 #3
You have duplicated the same problem. Your For loop simply copies the
references to the new vegetable collection.

Nov 21 '05 #4
Dave J wrote:

What Matt said is correct.

Dim Vegetables As New Collection
Dim NewVegetables As New Collection

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoadVeggies()
NewVegetables = Vegetables
The line above simply assigns the reference to the Vegetable collection
to the NewVegetables collection. In other words, they both point to
the *same* collection. You need this line:

NewVegetables = New VegetableCollection

Do as Matt suggested but make sure you are making copies of the
vegetable objects by adding a Clone method to your Vegetable class (see
below):

For Each v As Vegetable In Vegetables
Dim NewVeg As Vegetable = v.Clone
NewVegetables.Add(NewVeg)
Next
Dim Vegetable0 As New Vegetable
For Each Vegetable0 In NewVegetables
If Vegetable0.Name = "Squash" Then
Vegetable0.Color = "Green"
End If
Next
End Sub Private Sub LoadVeggies()
Dim vegetableA As New Vegetable
vegetableA.Name = "Carrot"
vegetableA.Color = "Orange"
Vegetables.Add(vegetableA)

Dim vegetableB As New Vegetable
vegetableB.Name = "Squash"
vegetableB.Color = "Yellow"
Vegetables.Add(vegetableB)
You then need to add a Clone method to your Vegetable class:

Public Function Clone() As Vegetable
Dim v As New Vegetable
v.Name = Me.Name
v.Color = Me.Color
Return v
End Function
End Sub
End Class


This should fix the problem.

Chris

Nov 21 '05 #5
Thanks Chris:
I got your solution to work, but omitted the line:
NewVegetables = New VegetableCollection
my system didn't like this or variations such as
NewVegetables = New Vegetables Collection
Its kind of a moot pt, in that the code is running, but can you clarify on
what this line was intended to do.
thanks

"Chris Dunaway" wrote:
Dave J wrote:

What Matt said is correct.

Dim Vegetables As New Collection
Dim NewVegetables As New Collection

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoadVeggies()
NewVegetables = Vegetables


The line above simply assigns the reference to the Vegetable collection
to the NewVegetables collection. In other words, they both point to
the *same* collection. You need this line:

NewVegetables = New VegetableCollection

Do as Matt suggested but make sure you are making copies of the
vegetable objects by adding a Clone method to your Vegetable class (see
below):

For Each v As Vegetable In Vegetables
Dim NewVeg As Vegetable = v.Clone
NewVegetables.Add(NewVeg)
Next
Dim Vegetable0 As New Vegetable
For Each Vegetable0 In NewVegetables
If Vegetable0.Name = "Squash" Then
Vegetable0.Color = "Green"
End If
Next
End Sub

Private Sub LoadVeggies()
Dim vegetableA As New Vegetable
vegetableA.Name = "Carrot"
vegetableA.Color = "Orange"
Vegetables.Add(vegetableA)

Dim vegetableB As New Vegetable
vegetableB.Name = "Squash"
vegetableB.Color = "Yellow"
Vegetables.Add(vegetableB)


You then need to add a Clone method to your Vegetable class:

Public Function Clone() As Vegetable
Dim v As New Vegetable
v.Name = Me.Name
v.Color = Me.Color
Return v
End Function

End Sub
End Class


This should fix the problem.

Chris

Nov 21 '05 #6
I just assumed you had some sort of Collection class so I guessed at
the name 'VegetableCollection'. It was just a guess on my part.
Somewhere you need to create a new 'Vegetables' container using
whatever name is appropriate for your project.

Nov 21 '05 #7

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

Similar topics

1
by: Mark | last post by:
Is there an online site where the various methods of storing arrays of classes and variables are explained? For example, I'd love a grid that showed the various pluses and minues of an Array,...
3
by: Guy Dillen | last post by:
Instead of using DataSets i want to implement an objects collection (in C#). E.g. a class Person and the instances are stored in a collection of Persons. So there is a layer that does the database...
3
by: Rob Thomas | last post by:
Hi, I've been tasked to come up with a new architecture for a large application at one of my customer's sites. In the past, I have developed multi-tier applications whereby the business...
2
by: a | last post by:
The problem: I want to allow an administrator (user) to create a list of teachers, each teacher in turn has a list of classes, each class has a list of students. There's an article at...
5
by: Simon | last post by:
Hi all, I am writing a windows application using vb.net on the 1.1 framework. We have in the application, some strongly typed collections that have been written as classes that do not inherit...
9
by: Ugur ASLAN | last post by:
Hello, I want to implement a class structure that can take a querystring and the value of querysting can be called as myClass.getQuery("test").value. I know that I can do it like...
16
by: 3rdshiftcoder | last post by:
hi- where would someone find things like lists, sets, map classes like a hash map or tree map. not specifically those but in general is there a book section that deals with it? in java you can...
4
by: Sid Price | last post by:
Hello, I have a class of objects (Device) that are managed by another object (Devices) with a collection class (DeviceCollection) inherited from Collections.Hashtable. Each of the Device objects...
3
by: fizilla | last post by:
Hello all! I have the following weird problem and since I am new to Python I somehow cannot figure out an elegant solution. The problem reduces to the following question: How to pickle a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.