473,851 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons.CollectionB ase

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.I tem(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 1380
Hi there,

Are any of the properties declared as "Shared"?

Nick.

"JonZ" <Jo**@discussio ns.microsoft.co m> wrote in message
news:9D******** *************** ***********@mic rosoft.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.Collecti ons.CollectionB ase
.
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.I tem(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**@discussio ns.microsoft.co m> 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.Collecti ons.CollectionB ase

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.Collecti ons.CollectionB ase

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
1591
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? Scenario: Our network folks don't want us installing IIS on all of our machines. ASP.NET requires IIS to be installed. Does IIS need to be on the developing machine? We are currently getting the error "Visual Studio .NET has detected
4
1458
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 sort of do...) his changes. Thanks.
8
3405
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 it up as C:\domains\appA. Locally, my IIS root points to C:\domains. I don't point it to C:\domains\appA since if I have an appB under C:\domains I wouldn't be able to get to it. So to access it via my browser I go to localhost/appA.
1
1901
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 at all. Now, I'm trying to deploy the app I done to a windows 2003 server, with IIS 6.0 and in wich I installed the Visual Studio 2003 server components (for web development). It has ASP.Net 1.1 too.
55
6280
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 C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
2
2100
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 "a&", then compiler complains reference to the local variable "a". 2. if the return type is "a", then everything works fine.
7
6693
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; } //---------------------------------------------------------------------------
3
3700
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 question is, as the local db is named 'local,' is there another name I need to give it and thus call from the connection page? Also, when copying a db, are the permissions (username and pass) copied over with it? Thanks,
1
2432
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 question is, as the local db is named 'local,' is there another name I need to give it and thus call from the connection page? Also, when copying a db, are the permissions (username and pass) copied over with it? Thanks,
0
11020
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10735
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9506
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7907
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5736
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5934
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4143
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.