Goal: filling some TextBoxes
Problem: when the first TextBox is filled, the values of the next ones are compromised. It happens in three steps.
First step. Say I have to fill two TextBoxes. A public function does this:
Public Sub FillingTextBoxes(Name As String)
'Fetching my object from a collection
Dim newObject As MyClass = MyCollection.Item(Name)
'Filling two textboxes
With newObject
TextBox1.Text = .Property1.ToString
TextBox2.Text = .Property2.ToString
MyCollection is a public Microsoft.VisualBasic.Collection.
Second step. Filling TextBox1 triggers a TextChanged event. Another public function changes the values of the same object:
Public Sub SomeOtherFunction(Name As String)
Dim newObject As MyClass = MyCollection.Item(Name)
newObject.Property2 = "something else"
Third step, here it comes. When SomeOtherFunction is done running, back in FillingTextBoxes, the value of newObject.Property2 is now "something else", even though this happened in another function.
How could I possibly solve this?
If what you are storing in the collection is a custom class then you need to implement a
clonefunction that allows a deep copy.Clone funcionality allows you to take an object reference and return a new copy of that same type that is a new reference to a different object. For example if you had this:
Then you could call for a new deep copy like this:
And you would have no problems because you are using a new copy of the same object instead of the reference in the collection.