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. 2 1234
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Arthur Dent |
last post by:
How do you sort a generic collection derived from
System.Collections.ObjectModel.Collection?
Thanks in advance,
- Arthur Dent
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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,...
| |