# Reference to old version of object

**URL:** <https://forum.xojo.com/t/reference-to-old-version-of-object/12908>\
**Category:** General\
**Created:** [December 10, 2013, 10:23pm UTC](https://forum.xojo.com/t/reference-to-old-version-of-object/12908 "2013-12-10T22:23:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oliver\_Scott-Brown](https://forum.xojo.com/letter_avatar_proxy/v4/letter/o/8baadc/32.png) [@Oliver\_Scott-Brown](https://forum.xojo.com/u/Oliver_Scott-Brown)\
**Post date:** [December 10, 2013, 10:23pm UTC](https://forum.xojo.com/t/reference-to-old-version-of-object/12908/1 "2013-12-10T22:23:32Z")

</div>

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:

```auto
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:

```auto
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

---

<div class="post-metadata">

**Author:** ![Paul\_Lefebvre](https://forum.xojo.com/user_avatar/forum.xojo.com/paul_lefebvre/32/3152_2.png) [@Paul\_Lefebvre](https://forum.xojo.com/u/Paul_Lefebvre)\
**Post date:** [December 10, 2013, 10:44pm UTC](https://forum.xojo.com/t/reference-to-old-version-of-object/12908/2 "2013-12-10T22:44:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Will\_Shank](https://forum.xojo.com/letter_avatar_proxy/v4/letter/w/f475e1/32.png) [@Will\_Shank](https://forum.xojo.com/u/Will_Shank)\
**Post date:** [December 10, 2013, 10:55pm UTC](https://forum.xojo.com/t/reference-to-old-version-of-object/12908/3 "2013-12-10T22:55:16Z")

</div>

> [@52083:@Oliver Scott-Brown](#):
>
> How do I assign a variable that only refers to an object by the state that it is in when it is referred to

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.

> [@52083:@Oliver Scott-Brown](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 4:58pm UTC](https://forum.xojo.com/t/reference-to-old-version-of-object/12908/4 "2020-10-29T16:58:48Z")

</div>


