Re: = operator between collections

Tech-Archive recommends: Fix windows errors by optimizing your registry



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
.



Relevant Pages

  • Re: Why Lisp?
    ... Is that bit about the empty list being set-theoretic fals ... because the result is a circular reference. ... Those sentences are the parens around the empty set in the ...
    (comp.lang.lisp)
  • Re: Net::SFTP::Attributes
    ... no matter what I do dealing with $ref always seems to be ... "empty" is a term that applies to lists, arrays, and ... be called and given a reference to a hash with three keys: ...
    (comp.lang.perl.modules)
  • Re: C# Fundamentals Part 3: ReferenceEquals question
    ... |> take a look at the remark clausein the Framework reference guide. ... What is an "Empty ... String.Empty is a public static field of type String initialized ... empty string object. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: newbie trouble making array of instances
    ... an Empty instance is initialized." ... string x = string.Empty; ... "" is not reference equal to string.Empty. ... The old instance of str1 was replaced with a whole new instance. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: initialize list collection
    ... PropertyForeignKeyCollection propertyForeignkeys = new ... reference types), and assigning that reference to your variable will cause ... the GC will eventually reclaim the memory used by that instance (that's ... want to do is empty the collection. ...
    (microsoft.public.dotnet.languages.csharp)