Firstly, I am aware of the similar question here: Assignment "=" operator in VB.NET 1.1
Suppose I have a VB.NET structure which contains an ArrayList:
Structure MarbleCollection
Private marbles As ArrayList
End Structure
Class Marble
Public name As String
Public radius As Double
End Class
I want to understand what happens when I create two instances of MarbleCollection and assign one to the other using the "=" operator:
marbleCol1 = marbleCol2
According to the accepted answer to the question linked above:
It's a reference copy, if the type is a reference type (ie: classes). If it's a value type (Structure), it will do a member by member copy.
When a member-by-member 'copy' is carried out (when assigning a value type), is VB.NET really just recursively calling the assignment operator on each of the members? If so, then assigning one MarbleCollection here to another will not produce a deep copy because the assignment operator between two marbles ArrayLists will produce a reference copy. I want a deep copy.
Is there a way that I can implicitly make a reference-type object copy itself like a value-type one? For example, I would find it useful to extend the functionality of the ArrayList class, but I don't want my subclass to copy its ArrayList values by reference, but instead value.
Take this concrete example:
Class BookList
Inherits ArrayList
End Class
Is there something I can do to the BookList class (i.e. implement an interface) that will make it so that:
books1 = books2
Will replicate the values inside books2 and assign the replicant references to books1?
I'm not looking to be spoon-fed the solution here and move on; I would appreciate an explanation of how the "=" operator actually functions and decides internally how it will carry out the assignment (and how I can influence it). Generalisation to other languages is welcome.
Edit:
There seems to be some confusion. I realize that it's not possible/advisable to overload the "=" operator. I want to understand how the operator works. I can then determine for myself how/if I can perform a deep copy by typing an assignment statement.
For instance, this may or may not be a correct, fully descriptive definition of the operator's behaviour:
- class1 = class2 - copies the reference of class2 to class1. They're the same instance.
- struct1 = struct2 - performs struct1.member = struct2.member for each member in struct2.
- primitive1 = primitive2 - creates a new primitive, replicates the value of primitive2 and writes the same value to the new primitive, and gives primitive1 the reference of this newly created primitive.
I'm looking for a definitive outline of behaviour as above.