473,503 Members | 3,045 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Collection

Dear colleagues,

I have a collection which stores arrays of numbers.
I do the following to fill the elements of the collection:
Dim array1() As Integer = {1, 2, 3}
Dim array2() As Integer = {4, 5, 6}
Dim my_collection As New Collection
my_collection.Add(array1, “Variable1”)
my_collection Add(my_collection.Item(“Variable1”), “Variable2”)
my_collection.Add(array2, “Variable3”)
Dim array3() As Integer = my_collection.Item(“Variable1”)
Dim array4() As Integer = my_collection.Item(“Variable3”)
array3(0) = array4(0)
my_collection.Remove(“Variable1”)
my_collection.Add(array3, “Variable1”)

So I should get this:
Variable1 = [4 2 3]
Variable2 = [1 2 3]
Variable3 = [4 5 6]

But instead I get the following:
Variable1 = [4 2 3]
Variable2 = [4 2 3]
Variable3 = [4 5 6]

The value of Variable2 changes as well. And I do not want that.
Any advice in how to solve this problem?

Thanks in advance,
Emilia.

Jul 21 '05 #1
2 1241
this statement:
my_collection Add(my_collection.Item("Variable1"), "Variable2") does not make a copy. It simply adds a reference to the same heap location
that contains the original array.

Therefore, this statement, which changes the original array: array3(0) = array4(0)
will also change the copied space.

Since your arrays contain value types, you can create a shallow copy of your
array using the Array.Clone() method, which will give you the desired
behavior.
Change this statement: my_collection Add(my_collection.Item("Variable1"), "Variable2")
to this my_collection Add(my_collection.Item("Variable1").clone(), "Variable2")
Note: if you use Clone to create a shallow copy of an array, where the array
contained objects, it would only copy the object references. You would not
have complete independence even then. The fact that your array doesn't
contain objects allows you to use .Clone effectively.

Hope this helps,

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Emilia" <Em****@discussions.microsoft.com> wrote in message
news:D6**********************************@microsof t.com... Dear colleagues,

I have a collection which stores arrays of numbers.
I do the following to fill the elements of the collection:
Dim array1() As Integer = {1, 2, 3}
Dim array2() As Integer = {4, 5, 6}
Dim my_collection As New Collection
my_collection.Add(array1, "Variable1")
my_collection Add(my_collection.Item("Variable1"), "Variable2")
my_collection.Add(array2, "Variable3")
Dim array3() As Integer = my_collection.Item("Variable1")
Dim array4() As Integer = my_collection.Item("Variable3")
array3(0) = array4(0)
my_collection.Remove("Variable1")
my_collection.Add(array3, "Variable1")

So I should get this:
Variable1 = [4 2 3]
Variable2 = [1 2 3]
Variable3 = [4 5 6]

But instead I get the following:
Variable1 = [4 2 3]
Variable2 = [4 2 3]
Variable3 = [4 5 6]

The value of Variable2 changes as well. And I do not want that.
Any advice in how to solve this problem?

Thanks in advance,
Emilia.

Jul 21 '05 #2
Thank you very much for your help!!! Now it works!!!

"Nick Malik [Microsoft]" wrote:
this statement:
my_collection Add(my_collection.Item("Variable1"), "Variable2")

does not make a copy. It simply adds a reference to the same heap location
that contains the original array.

Therefore, this statement, which changes the original array:
array3(0) = array4(0)


will also change the copied space.

Since your arrays contain value types, you can create a shallow copy of your
array using the Array.Clone() method, which will give you the desired
behavior.
Change this statement:
my_collection Add(my_collection.Item("Variable1"), "Variable2")


to this
my_collection Add(my_collection.Item("Variable1").clone(), "Variable2")


Note: if you use Clone to create a shallow copy of an array, where the array
contained objects, it would only copy the object references. You would not
have complete independence even then. The fact that your array doesn't
contain objects allows you to use .Clone effectively.

Hope this helps,

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Emilia" <Em****@discussions.microsoft.com> wrote in message
news:D6**********************************@microsof t.com...
Dear colleagues,

I have a collection which stores arrays of numbers.
I do the following to fill the elements of the collection:
Dim array1() As Integer = {1, 2, 3}
Dim array2() As Integer = {4, 5, 6}
Dim my_collection As New Collection
my_collection.Add(array1, "Variable1")
my_collection Add(my_collection.Item("Variable1"), "Variable2")
my_collection.Add(array2, "Variable3")
Dim array3() As Integer = my_collection.Item("Variable1")
Dim array4() As Integer = my_collection.Item("Variable3")
array3(0) = array4(0)
my_collection.Remove("Variable1")
my_collection.Add(array3, "Variable1")

So I should get this:
Variable1 = [4 2 3]
Variable2 = [1 2 3]
Variable3 = [4 5 6]

But instead I get the following:
Variable1 = [4 2 3]
Variable2 = [4 2 3]
Variable3 = [4 5 6]

The value of Variable2 changes as well. And I do not want that.
Any advice in how to solve this problem?

Thanks in advance,
Emilia.


Jul 21 '05 #3

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

Similar topics

8
2186
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in...
5
2710
by: Kurt Bauer | last post by:
I have an ASP group calendar application which pulls calendar data from Exchange via webdav into an XML string. I then loop the XML nodes to populate a collection of appointments. Finally I use...
18
5717
by: Scott | last post by:
I have a collection where the items in the collection are dates. I want to iterate over the collection and build a value list string for the rowsource of a listbox. The dates in the collection are...
11
18684
by: Pavils Jurjans | last post by:
Hello, There's some confusion about the purpose and difference between these handy classes... First, both of them are holding number of key - value pairs, right? Then, I see that there may be...
2
3155
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
6
4825
by: Michael D. Ober | last post by:
In VB 6, the loop iterator v in the following code must be a variant. dim v as variant dim c as new collection for each v in collection ... next v What is the general translation in VB 7.1...
3
2142
by: Matt Michael | last post by:
I have a listview control and a collection object right now that I'm trying to pass information to and from. Whenever I click on the checkbox, I want it to remove certain listview items and add...
10
2748
by: Chet Cromer | last post by:
I am creating a set of base classes and sub classes to use throughout a program I'm developing. The base class represents a generic "lookup table" from my database that contains lists of things...
6
13372
by: Scott M. Lyon | last post by:
As I mentioned in my other post, I'm attempting to, using COM Interop so I can update existing VB6 code to (for several specific functions) return a Hashtable from a .NET library. I've had...
6
7190
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
0
7193
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
7067
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
7316
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
7449
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
5562
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,...
1
4992
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...
0
4666
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
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
371
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...

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.