473,785 Members | 2,807 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.A dd(array1, “Variable1” )
my_collection Add(my_collecti on.Item(“Vari able1”), “Variable2” )
my_collection.A dd(array2, “Variable3” )
Dim array3() As Integer = my_collection.I tem(“Variable 1”)
Dim array4() As Integer = my_collection.I tem(“Variable 3”)
array3(0) = array4(0)
my_collection.R emove(“Variab le1”)
my_collection.A dd(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 1261
this statement:
my_collection Add(my_collecti on.Item("Variab le1"), "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_collecti on.Item("Variab le1"), "Variable2" )
to this my_collection Add(my_collecti on.Item("Variab le1").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****@discuss ions.microsoft. com> wrote in message
news:D6******** *************** ***********@mic rosoft.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.A dd(array1, "Variable1" )
my_collection Add(my_collecti on.Item("Variab le1"), "Variable2" )
my_collection.A dd(array2, "Variable3" )
Dim array3() As Integer = my_collection.I tem("Variable1" )
Dim array4() As Integer = my_collection.I tem("Variable3" )
array3(0) = array4(0)
my_collection.R emove("Variable 1")
my_collection.A dd(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_collecti on.Item("Variab le1"), "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_collecti on.Item("Variab le1"), "Variable2" )


to this
my_collection Add(my_collecti on.Item("Variab le1").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****@discuss ions.microsoft. com> wrote in message
news:D6******** *************** ***********@mic rosoft.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.A dd(array1, "Variable1" )
my_collection Add(my_collecti on.Item("Variab le1"), "Variable2" )
my_collection.A dd(array2, "Variable3" )
Dim array3() As Integer = my_collection.I tem("Variable1" )
Dim array4() As Integer = my_collection.I tem("Variable3" )
array3(0) = array4(0)
my_collection.R emove("Variable 1")
my_collection.A dd(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
2216
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 the process), I looked up the STL code. Erase certainly does not delete the memory associated with the element. However, it appears that the destructor on the element is invoked. I wonder why it has to be this way. In my opinion, this renders...
5
2734
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 the appointment collection to populate the calendar control. The performance getting the XML data is fine, but loading the data into the collection is slow. My question/problem is should I be using the collection, a dataset, or something else to...
18
5752
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 not in chronological order. Is there a way to first sort the collection and put the dates in chronological order before creating the value list string? Or, how would I iterate over the collection pulling out the dates in chronological order? ...
11
18710
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 some difference in terms of data types allowed for keys and values, perhaps? I read the following in MSDN about "CollectionBase" class and
2
3183
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 problems that I have encountered to date and the solutions (if any) that I found. http://users.adelphia.net/~brianpclab/ServerControlCollectionIssues.htm This page also has all of the source code in a compressed file that you are free to download...
6
4841
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 and VB 8 Beta 1? Also, is there an easy way to force "v" to be an early bound variable. In VB 6 this can be
3
2154
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 them to the collection. Whenever I uncheck the checkbox, I want to add the items back into the listview from the collection, and remove them from the collection so I can do the process multiple times. The listview tag is being used as a key, so I...
10
2783
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 like manufacturers, makes, modes, etc. of cars. I have created a generic "datacollection" class and a generic "dataobject" class to represent the table and the rows within that table as a collection of objects with generic properties for...
6
13422
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 very little luck processing the Hashtable itself in VB6 (I can add a reference to the project so it knows what a Hashtable is, but I'm not having much luck looping through all objects in the Hashtable), so I decided to try a different idea.
6
7224
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
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
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...
1
7502
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
6741
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
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...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
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.