Reputation: 31
This is my first post, and I have been looking around, but cannot find an answer to my problem, as I'm not even sure the proper terminology for it, and I'm not entirely sure how to explain it, but I'll try through a tiny example that is essentially a simplified version of my actual code:
Public Class myCustomClass
Public myValue as String
Public Sub New()
With Me
.myValue = String.Empty
End With
End Sub
End Class
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim myObject_A As New myCustomClass
myObject_A.myValue = "Sample String"
Dim myObject_B As New myCustomClass
myObject_B = myObject_A
myObject_B.myValue = "New Sample String"
End Sub
The problem I am having is, when I change the value of myObject_B.myValue
to "New Sample String"
, the same change is made to myObject_A.myValue
, as though instead of copying the value from myObject_A
to myObject_B
, myObject_B
is acting like a pointer to myObject_A
.
What I want is to make myObject_B
the same value as myObject_A
, but not link the two together. I have only started noticing this behavior since upgrading to Visual Studio Express 2012 for Windows Desktop. In previous versions I don't think this is how my variables behaved.
I suspect I'm doing something wrong, and I also suspect something was changed in VS 2012 for Desktop. I'm more than willing to share my actual code if needed, I just didn't want to paste a bunch of stuff if the above was enough to figure out where I'm screwing up.
Sincerely, Brian
Update:
Thank you for the Cloning link, and coding example. It helps me understand what is being talked about, and gives me something to read up on further when I have more time. This is entirely new for me and I appreciate learning about it. It brings me to my next question, however, which is that I'm actually making lists of myCustomClass. Is it possible to implement cloning on an entire list, or will I need to create a list of New myCustomClass objects, then iterate through each list item and use the clone method on each one?
Dim myList as List(Of myCustomClass)
I'm not sure how to implement cloning for a list of myCustomClass.
Upvotes: 0
Views: 231
Reputation: 96
What I want is to make myObject_B the same value as myObject_A, but not link the two together.
The simplest way to achieve that would be to:
Dim myObject_A As New myCustomClass
Dim myObject_B As New myCustomClass
myObject_A.myValue = "Sample String"
myObject_B.myValue = myObject_A.myValue
This will not link them together but place myValue
from myObject_A
in to myObject_B
.
It is a quick thing to do, however, if you have complex objects, I would suggest implementing Clone()
method in your class.
More information on Cloning.
Update: I believe your updated question is answered here
Upvotes: 1
Reputation: 2119
Add a Clone
method in your myCustomClass
Public Class myCustomClass
Public myValue as String
Public Sub New()
With Me
.myValue = String.Empty
End With
End Sub
Public Function Clone() As myCustomClass
Return DirectCast(Me.MemberwiseClone, myCustomClass)
End Function
End Class
And assign your myObject_B
like this ...
myObject_B = myObject_A.Clone()
Reference: Object.MemberwiseClone method
Upvotes: 1
Reputation: 755289
In this case myClass
is a Class
instance and has pointer like semantics. Assigning between two instances will just cause them to refer to the same object it won't create independent copies. If you want to have value behavior then make myClass
a Structure
instead of a Class
Upvotes: 1