By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,682 Members | 1,999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,682 IT Pros & Developers. It's quick & easy.

Collection

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.