Reference to old version of object

How do I assign a variable that only refers to an object by the state that it is in when it is referred to, for example:

dim oldPicture as picture = changingPicture
'this code changes the oldPicture and the new changingPicture but I don't want to change the oldPicture
changingPicture.Graphics.DrawLine 0,0,12,12

Here is a way which I believe will allow me to change the changingPicture object without interfering with the oldPicture object:

dim oldPicture as new picture(changingPicture.width,changingPicture.height)
oldPicture.Graphics.DrawPicture 0,0,changingPicture
'changed the changingPicture but kept the old version of the picture
changinPicture.Graphics.DrawLine 0,0,12,12

I know I got some of the parameters mixed, this is because I did not write the code in the Xojo IDE. Coming to think about it, it would be cool if you could write and execute code straight from the forums, maybe they could use xojoscript somehow.

But beneath my rambling, I probably got this wrong and my bug may lie somewhere else. I have taken a different and better approach for the bug I was stuck with. But I am just curious as to how references work. Can you change objects that you have ,for example placed in an array, without refereeing through the array but the original object you declared to go in that array. Also, how would you go about doing exactly this but not have the changes apply.

I hope you understand, THANKS

In your first example, oldPicture is a reference to the instance of changingPicture. Since they both refer to the same thing, changing one changes the other.

If they need to be separate, then they need to be two different instances. You are correct that the way to do it is to create a new instance of oldPicture and copy changingPicture into it.

There’s no magic way to duplicate/copy/clone an instance. For your own classes you can add a duplicate method, for Picture you could create an extension method. When you write these methods you’ll see that for the most part it’s just copying values over to a new instance but there may be parts you don’t copy or copy a property reference instead of duplicating. It’s because of this variation in what a duplicate might be that there’s no automatic built in mechanism.

You can have multiple references to an instance.

dim obj, arr() As MyObject
obj = new MyObject
arr. append obj
arr. append new MyObject
//both obj and arr(0) refer to the same instance
obj = arr(1)
//now obj and arr(1) are the same