views:

48

answers:

2

I sort of understand why this is happening, but not entirely. I have a base class with a Shared (Static) variable, declared like so:

Public Shared myVar As New MyObject(arg1, arg2)

In a method of a derived class, I set a local variable like so:

Dim myLocalVar As MyObject = myVar

Now when I do something like myLocalVar.Property1 += value, the value in Property1 persists to the next call of that method! I suppose I get why it would be happening; myVar is being set by reference instead of by value, but I've never encountered anything like this before. Is there any way (other than my workaround which is to just create a new object using the property values of myVar) to create myLocalVar by value?

+3  A: 

When you create myLocalVar you are creating a new reference to the same shared object. If you truly want a local copy of the shared instance you will need to create a true copy.

This is done by either cloning the instance or with a copy constructor on the type that allows you to create a copy of the instance. This is not as simple as it sounds, however, due to the differences between deep and shallow copying and a cloned or copied instance could create similar problems for you if the property you are accessing is simply a shallow-copied reference to the same instance that the property on the original instance is referencing.

The best thing I to do in this case is to create a local copy of only the parts of the shared instance that you need, rather than copying the entire object graph. This means create a local copy of whatever type Property1 is and using that.

Andrew Hare
yeah, this was my workaround, actually. What is the difference between "deep" and "shallow" copying? Thanks for this very knowledgeable answer!
Jason
A shallow copy means to copy just the object's "top level" properties. A deep copy would mean to copy both the top level properties and to dig into the sub-properties of the properties, etc.
Eilon
@Jason: Brad Abrams has a good explanation here: http://blogs.msdn.com/brada/archive/2003/04/09/49935.aspx "Deep-copy copies the cloned object and all objects referenced by the object, recursively until all objects in the graph are copied. A non-deep copy (referred to as ‘shallow’ if only the top level references are copied) may do none, or part of a deep copy."
Andrew Hare
A: 

An interesting side note: In modern languages starting with C++ pointers were replaced by objects. Prior to that (eg in C and Pascal), you had to manipulate the pointers (to what were basically objects) yourself. Because objects are actually pointers to data they "contain" they are always references. Object based languages try to shield you from this reality but sometimes, like in this case, the reality shines through.

If you had ever had to implement a pointer heavy program in C or Pascal the reality of the underlying manipulation would be much clearer. For better or worse, most programmers these days have not had that experience.

Hogan
-1 Objects are not a replacement for pointers - pointers are a type of reference that allow you to access memory at a specific location. In C++, pointers are used to access objects.
Andrew Hare
I won't flag you, and I can't give you -1, but that is not what I said. What I said was, object are actually pointers to data they contain. This is true. When you implement an object oriented programing language objects are implemented as pointers to struct (with the jump table and then the data). If you would like books on the subject I'd be happy to give you some references. Or I could send you code of a compiler I wrote.
Hogan