Reference vs complete copy


I remember seeing some where that when you create multiple references of an object and copy one to the other pointers are created instead of a physical copy. I need to avoid this situation and I don’t recall how to avoid it when I am using json.

I am creating temporary json structures and then adding them to a single multilevel structure. When I initialize the temp structure (after copying to the main structure) it also get deleted from the main structure?

Dim TmpJ As New Xojo.Core.Dictionary
TmpJ = New Xojo.Core.Dictionary

Dim Rates As Xojo.Core.Dictionary
Rates = New Xojo.Core.Dictionary

TmpJ.Value("abc_options") = Array("Test Data")
Rates.Value("xyz_options") = TmpJ

Rates.Value("moretest") = "12345678"
MsgBox(json2text(Rates)) // All is good to here
MsgBox(json2text(Rates)) // "abc_options" has been eliminated

Pretty sure this is happening because anything in TmpJ that is copied to Rates is only referenced. So when TmpJ is wiped so are the copied items to Rates.

How do I avoid this? Or is there a better way. I find it tricky to build multiple levels in a single json structure.

Before ( what I want)
{“moretest”:“12345678”,“xyz_options”:{“abc_options”:[“Test Data”]}}

After (not what I want)

create a CLONE method in the original object that returns an instance of the control with all the properties set using the original

Function Clone() as myControl 
dim temp as new myControl
temp.prop1 = self.prop1
temp.prop2 = self.prop2
return temp
End Function
dim newInstance as new myControl

Or just use the Xojo.Core.Dictionary clone method

That would make a copy of the references, but not the objects themselves. So it may or may not achieve the OP’s goal, depending on how he used it.

These will work but not exactly what I was looking for. The problem is I want to dynamically build a multi level json and I won’t always know the structure up front. If I did I could start with a skeleton. So I may be in a situation where I need to create something like this on the fly.

 "level1": {
        "data1": "none",
        "level2": {
            "data2": "documents",
            "data21": [
                    "data22": "xx",
                    "data23": "some data",
                    "data24": "1.503",
                    "data25": 1,
                    "data26": 50
            "data27": "return_to_sender"
        "data11": "testing"

It’s easy enough to create a single layer json structure and include them one by one. That could get messy in some cases. Is there a better way to create a json key when some of the layers are missing.

Michael - take a look at Kem’s class referenced here:

Class repo:

it might help.

This might help.

Instead of doing:


Just do:

TmpJ = New Xojo.Core.Dictionary

which creates a whole new instance that is empty for you to fill up without touching the existing one. The existing one is held onto by the Rates dictionary because you had previously done:

Rates.Value("xyz_options") = TmpJ

With reference types like this as long as something has a reference then it won’t disappear.

The thing is when you do:

Rates.Value("xyz_options") = TmpJ

you are not copying Tmpj, you are creating a reference (what we used to call a pointer :slight_smile: to it. The number of dictionaries that exist is equal to the number of times you do “new”.

Jason is right. Set the reference and create a new one.