I have custom class like the below;
Public MyClass
Public Property MyText() As String
End Class
Then in my code page I have the following VB.NET code;
Dim obj1 As New MyClass
Dim obj2 As New MyClass
obj1 = obj2
obj1.MyText = "Test"
My problem is that when the below piece of code is executed the obj2.MyText is updated as well. How can I avoid this?
obj1.MyText = "Test"
Thanks.
This is the problem:
That copies the value of
obj2toobj1. That value is not the object – it’s a reference to the object. So nowobj1andobj2refer to the same object, so any changes you make via one variable will be seen by the other.I realize this is VB rather than C#, but you may still find my article on reference types and value types useful. Fundamentally, you need to grok how reference types behave.