By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,606 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,606 IT Pros & Developers. It's quick & easy.

Local copy of variable not acting locally

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi there,

Are any of the properties declared as "Shared"?

Nick.

"JonZ" <Jo**@discussions.microsoft.com> wrote in message
news:9D**********************************@microsof t.com...
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

Nov 21 '05 #2

P: n/a
"JonZ" <Jo**@discussions.microsoft.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3

P: n/a
> 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
Nov 21 '05 #4

P: n/a
> '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!
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.