Re: = operator between collections
- From: Armin Zingler <az.nospam@xxxxxxxxxx>
- Date: Tue, 12 Jul 2005 14:22:18 +0200
Vasilis X schrieb:
Hello. Here is the situation I am facing.
I have 2 collections, A and B. Both have various elements (string type) in them.
I want to reset them, empty all elements from these collections. I do this by typing :
Dim Empty as new Collection A=Empty B=Empty
At first this works ok and both A and B are empty.
But...
when I add an element to collection B it is also added to collection A, eg
B.add ("Testing element")
will produce as a result A.Count=1 instead of 0.
Why is this happening?
Is it because with the commands A=x, B=x both A and B refer to the same position of memory that holds values of x ?
Right. Empty points to a new (and empty) collection. By assigning 'empty' to 'A' and to 'B' you copy the content of 'empty' into both variables. As the content of 'empty' is a reference to the collection, now also A and B point to the same collection. If you want to have A and B point to different collections, you must create different collections:
a = new collection b = new collection
A collection is a "reference type" which means that variables of that type contain only the reference (= the position of the object in memory). In opposite, there are "value types", like Integer, Double and Structure types. Copying these variables also means copying the variable content, but this is the object itself, not the reference to the object.
Armin .
- References:
- = operator between collections
- From: Vasilis X
- = operator between collections
- Prev by Date: = operator between collections
- Next by Date: Re: = operator between collections
- Previous by thread: = operator between collections
- Next by thread: Re: = operator between collections
- Index(es):
Relevant Pages
|