= operator between collections

V

Vasilis X

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 ?

Please help and if possible clarify this.

Thanks in advance
Vasilis.
 
A

Armin Zingler

Vasilis said:
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
 
R

Robin Tucker

Dim Empty As New Collection, will create one new instance of a Collection
object. You are assigning a reference to Empty to both A and B.
Effectively they are "pointing" at the same instance (in C++ speak).

If, however, you write:

A = New Collection
B = New Collection

or

Dim Empty1 As New Collection, Empty2 As New Collection

A = Empty1
B = Empty2

then you have created two instances of a collection, one for each of A and
B.

Hope this makes sense :)
 
C

CT

The Collection data type is a reference type and stored on the managed heap.
Reference type variables are not intrinsically tied to a fixed memory
location that always has a value like value type variables (Integer, String,
etc.). You can access the value of a reference type via a reference to the
memory location holding that value. This is because, the variable does not
hold the bits of the object, but refers to the bits of the object located in
the memory.

So, to cut a long story short, you've created an instance of the Collection
class and made two variables reference the collection. Well, you actually
have three references to the same object, Empty, A, and B.
 

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