473,668 Members | 2,456 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
LoadVeggies()
NewVegetables = Vegetables
Dim Vegetable0 As New Vegetable
For Each Vegetable0 In NewVegetables
If Vegetable0.Name = "Squash" Then
Vegetable0.Colo r = "Green"
End If
Next
End Sub
Private Sub LoadVeggies()
Dim vegetableA As New Vegetable
vegetableA.Name = "Carrot"
vegetableA.Colo r = "Orange"
Vegetables.Add( vegetableA)

Dim vegetableB As New Vegetable
vegetableB.Name = "Squash"
vegetableB.Colo r = "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 1250
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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
LoadVeggies()
For Each Veg as Vegetable In Vegetables
NewVegetables.A dd(Veg)
Next
Vegetables.Clea r
For Each Vegetable0 As Vegetable In NewVegetables
If Vegetable0.Name = "Squash" Then
Vegetable0.Colo r = "Green"
End If
Next
End Sub
Private Sub LoadVeggies()
Dim vegetableA As New Vegetable
vegetableA.Name = "Carrot"
vegetableA.Colo r = "Orange"
Vegetables.Add( vegetableA)

Dim vegetableB As New Vegetable
vegetableB.Name = "Squash"
vegetableB.Colo r = "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(ByVa l sender As System.Object, ByVal e As
System.EventAr gs) Handles MyBase.Load
LoadVeggies()
NewVegetables = Vegetables
Dim Vegetable0 As New Vegetable
For Each Vegetable0 In NewVegetables
If Vegetable0.Name = "Squash" Then
Vegetable0.Colo r = "Green"
End If
Next
End Sub
Private Sub LoadVeggies()
Dim vegetableA As New Vegetable
vegetableA.Name = "Carrot"
vegetableA.Colo r = "Orange"
Vegetables.Add( vegetableA)

Dim vegetableB As New Vegetable
vegetableB.Name = "Squash"
vegetableB.Colo r = "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 "newvegtabl es" 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.clea r" 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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
LoadVeggies()
For Each Veg as Vegetable In Vegetables
NewVegetables.A dd(Veg)
Next
Vegetables.Clea r
For Each Vegetable0 As Vegetable In NewVegetables
If Vegetable0.Name = "Squash" Then
Vegetable0.Colo r = "Green"
End If
Next
End Sub
Private Sub LoadVeggies()
Dim vegetableA As New Vegetable
vegetableA.Name = "Carrot"
vegetableA.Colo r = "Orange"
Vegetables.Add( vegetableA)

Dim vegetableB As New Vegetable
vegetableB.Name = "Squash"
vegetableB.Colo r = "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(ByVa l sender As System.Object, ByVal e As
System.EventAr gs) Handles MyBase.Load
LoadVeggies()
NewVegetables = Vegetables
Dim Vegetable0 As New Vegetable
For Each Vegetable0 In NewVegetables
If Vegetable0.Name = "Squash" Then
Vegetable0.Colo r = "Green"
End If
Next
End Sub
Private Sub LoadVeggies()
Dim vegetableA As New Vegetable
vegetableA.Name = "Carrot"
vegetableA.Colo r = "Orange"
Vegetables.Add( vegetableA)

Dim vegetableB As New Vegetable
vegetableB.Name = "Squash"
vegetableB.Colo r = "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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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 VegetableCollec tion

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.A dd(NewVeg)
Next
Dim Vegetable0 As New Vegetable
For Each Vegetable0 In NewVegetables
If Vegetable0.Name = "Squash" Then
Vegetable0.Colo r = "Green"
End If
Next
End Sub Private Sub LoadVeggies()
Dim vegetableA As New Vegetable
vegetableA.Name = "Carrot"
vegetableA.Colo r = "Orange"
Vegetables.Add( vegetableA)

Dim vegetableB As New Vegetable
vegetableB.Name = "Squash"
vegetableB.Colo r = "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 VegetableCollec tion
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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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 VegetableCollec tion

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.A dd(NewVeg)
Next
Dim Vegetable0 As New Vegetable
For Each Vegetable0 In NewVegetables
If Vegetable0.Name = "Squash" Then
Vegetable0.Colo r = "Green"
End If
Next
End Sub

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

Dim vegetableB As New Vegetable
vegetableB.Name = "Squash"
vegetableB.Colo r = "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 'VegetableColle ction'. 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
1382
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, ArrayList, Collection, HashTable, Queue, Stack, etc ... Thanks! Mark
3
1297
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 access and mapping of relatoinal <-> objects. The Persons collections should have all the necessary possibilities: Add, Modify, Delete, Query. Has anyone suggestions for documents, books on this topic? Thanks, Guy
3
1719
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 objects maintain the database using stored procedures etc and provide the data to the GUI layer via a set of objects and collections. After using the typed datasets with .NET, it appears that you van provide the same functionality as objects and...
2
1412
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 http://aspalliance.com/721 that describes making classes for the Student and the StudentList, but I'm wondering how to make classes that are collections of the other classes, ie a ClassList that holds the StudentList class and a TeacherList class that...
5
4226
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 from collection base, but use an internal collection, to hold the objects and then implement IEnumerator, see example below,
9
1782
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 myClass.getQuery("test") and get the value of "test" querystring. But actually, this is an example only. In order to my class library organized I need a structure like this. I search internet and documents for ASP and Vbscript, as far as I found is that in...
16
5101
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 use the interface to these without having to know all of the implementation details. is there an interface to a library with these kind of methods in c++? i knew a little c++ from 2 really basic introductory courses. however, each time we...
4
3990
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 can raise an event and I need the managing class (Devices) to be able to catch these events. Public Class Device Public Event StatusChange()
3
6088
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 collections.defaultdict object that has set the default_factory property? For Example (from the IDLE console): >>> words = collections.defaultdict(lambda: 1) >>> f = file("temp","w")
0
8371
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8790
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8652
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7391
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4202
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2782
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1779
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.