Xojo.Core.Dictionary.Clone bug or misunderstanding?

Hi all,

I have had many users report that my app isn’t behaving properly.

I assume that when using Xojo.core.Dictionary.Clone, any changes to a value in the cloned dictionary will leave the “master” dictionary unchanged, at the condition that the value or key is not a class instance.
But this doesn’t seem to be the case.

According to the Xojo docs for Xojo.core.Dictionary.Clone

Is a Text array a class ?

The code has been simplified to the following:

Dim dic1, dic2 As Xojo.Core.Dictionary

//Initialize dic1
dic1 = new Xojo.Core.Dictionary

Dim tArray() As Text
tArray.Append "1"
tArray.Append "2"

dic1.Value("") = tArray

//Cloning dic1
//Assuming that cloning also clones all internal objects that aren't a class.
//Is a text array even an object??
dic2 = dic1.Clone

Dim tArray2() As Text = dic2.Value("")

//Removing one element from text array in dic2
//Which should leave dic1 unchanged because it was cloned

//Counting elements in each text array
Dim cnt1, cnt2 As Integer

Dim a() As Text
a = dic1.Value("")
cnt1 = a.Ubound

a = dic2.Value("")
cnt2 = a.Ubound

if cnt1 = cnt2 then
  Break //This shouldn't happen
end if

shallow copy. Your text array is referenced, not copied!

You could walk over the new dictionary contents and clone all arrays yourself.

I dealt with the same and finally did this. In fact you do the same with dates etc. if you want to get an independent copy rather than a reference.

I thought that arrays where copied too, just like any Text, String, Integer…

Thanks to both of you.

No arrays are not copied - they are a reference type

See the bunch of blogs posts about reference types I wrote