Collection

G

Guest

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.
 
N

Nick Malik [Microsoft]

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 said:
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.
 
G

Guest

Thank you very much for your help!!! Now it works!!!

Nick Malik said:
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 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top