473,382 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Local copy of variable not acting locally

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
4 1364
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
"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
> 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
> '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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Scott | last post by:
I have been told that the only way to develop an ASP.NET application is to do it on the localhost, and then just copy the files out to the web server that will be hosting. Is this true? ...
4
by: aualias | last post by:
Hi, Using ASP.NET, how can I compare my local version of a file to a version on the server? Another developer made changes to the file that I am working on and I do not want to lose (well, I...
8
by: nick | last post by:
I have a problem and I've been using a cheezy work around and was wondering if anyone else out there has a better solution. The problem: Let's say I have a web application appA. Locally, I set...
1
by: lauralucas | last post by:
Hello I'm using ASP.NET 1.1, Visual Studio 2003, IIS 5.1 and windows XP as development machine. I can work in this machine locally and create web apps that I can access via localhost. No problem...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
2
by: Jess | last post by:
Hello, I understand that if a function "f" has a local variable "a", and after it returns, "a" vanishes. If "f" returns "a" as a result, then I noticed the following: 1. if the return type is...
7
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; }...
3
by: ll | last post by:
Hi, I have copied a MS SQL database to my local machine and am testing it with ASP pages, locally, on IIS. I have a db connect page that keeps the current connections for the live database. My...
1
by: ll | last post by:
Hi, I have copied a MS SQL database to my local machine and am testing it with ASP pages, locally, on IIS. I have a db connect page that keeps the current connections for the live database. My...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.