Local copy of variable not acting locally

G

Guest

I want to declare a local copy of an object so I can modify the local
version without affecting the original copy. But when I update my local copy,
the original object changes, too.


My original object is a collection I’ve defined. I started with a class
definition:

Public Class MyClassDef
Inherits System.Collections.CollectionBase
…
End Class


Then I declared an instance of my collection:

Public MyMasterObject As New MyClassDef


I try to make a local copy of this collection to use only on one form. I
have declared a module level private variable like this:

Public Class MyForm
Inherits System.Windows.Forms.Form

Private MyLocalObject As New MyClassDef

Private Sub MySub1

MyLocalObject = MyMasterObject

End Sub

Private Sub MySub2

MyLocalObject.Item(0).Value = <new value>

End Sub

End Class


When I execute MySub2, my intention is to modify MyLocalObject without
affecting MyMasterObject. However, MyMasterObject is updated, too.

Please point me to a description of declaration/scope/referencing
information that will help me figure out how to separate my local copy from
my original object.

TIA,
JZ
 
H

Herfried K. Wagner [MVP]

JonZ said:
I want to declare a local copy of an object so I can modify the local
version without affecting the original copy. But when I update my local
copy,
the original object changes, too.

My original object is a collection I’ve defined. I started with a class
definition:

Public Class MyClassDef
Inherits System.Collections.CollectionBase
…
End Class


Then I declared an instance of my collection:

Public MyMasterObject As New MyClassDef


I try to make a local copy of this collection to use only on one form. I
have declared a module level private variable like this:

Public Class MyForm
Inherits System.Windows.Forms.Form

Private MyLocalObject As New MyClassDef

'As New' doesn't make sense here because you are assigning another instance
of 'MyClassDef' in 'MySub1'.
Private Sub MySub1

MyLocalObject = MyMasterObject

'MyClassDef' is a reference type, so no object is copied. Instead, after
the assignment both, 'MyLocalObject' and 'MyMasterObject' point to the same
object (instance). What you can do is implementing the 'ICloneable'
interface to extend your class by a 'Clone' method that can be used to
create a "copy" of the instance.
 
G

Guest

Are any of the properties declared as "Shared"?


No.

My collection class has only one, read-only property. I am trying to
implement the equivalent of an array of structures:

Public Class MyClassDef
Inherits System.Collections.CollectionBase

Public ReadOnly Property Item(ByVal index As Integer) As MyStruc
Get
Return CType(List.Item(index), MyStruc)
End Get
End Property

....<snip Add and Remove methods>
End Class



Public Class MyStruc
Public PropertyOne As String
Public PropertyTwo as String
End Class
 
G

Guest

'As New' doesn't make sense here because you are assigning another instance
of 'MyClassDef' in 'MySub1'.

Yes, that was just a vain attempt to fix my original problem. My real
code doesn't use the 'As New' there.
'MyClassDef' is a reference type, so no object is copied. Instead, after
the assignment both, 'MyLocalObject' and 'MyMasterObject' point to the same
object (instance).

That's what I suspected...
What you can do is implementing the 'ICloneable'
interface to extend your class by a 'Clone' method that can be used to
create a "copy" of the instance.

Ah yes, that's the help I need. A 'Clone' method does, indeed, fix my
problem. Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top