copy array - not by reference

enonod

Well-Known Member
Licensed User
Longtime User
I have a 2D array of objects and wish to copy it as a record such that it is not linked by reference to the original. I cannot find how to do this. Every test I try results in referenced data.
Any help please?
 

enonod

Well-Known Member
Licensed User
Longtime User
They are all of the same type and are user Type i.e.
Type Fred(x as Byte, y as Byte, M as boolean)

[EDIT] By record I meant a memory not a Database record
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use the new KeyValueStore class for that. The code is:
B4X:
Sub CopyFreds(Freds() As Fred) As Fred()
   kvs.PutObject("temp", Freds)
   Return kvs.GetObject("temp")
End Sub

Complete example:
B4X:
Sub Process_Globals
   Private kvs As KeyValueStore
   Type Fred(x As Byte, y As Byte, M As Boolean)
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      kvs.Initialize(File.DirInternalCache, "datastore")
   End If
   Dim freds(3) As Fred
   freds(0).x = 0
   freds(1).x = 1
   freds(2).x = 2
   
   Dim freds2() As Fred = CopyFreds(Freds)
   freds2(0).x = 3
   
End Sub

Sub CopyFreds(Freds() As Fred) As Fred()
   kvs.PutObject("temp", Freds)
   Return kvs.GetObject("temp")
End Sub

http://www.b4x.com/forum/showthread.php?p=152277
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Thank you for your time Erel, in providing that comprehensive example.
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
It seems that I asked the wrong question but got a right answer that is not really suitable for the need.
I rephrase...
I need to copy a few elements of an array by value not by reference
They are all of the same type and are user Type i.e.
Type Fred(x as Byte, y as Byte, M as boolean)
such that they do not change after copying, when the original changes.
The purpose is for an Undo system and thus a filing solution is not really suitable. I saved the items as keys and values in a Map but when the original changes (the purpose of the undo) then of course so do the copies.

Is there a simple way of copy by value please?
[Edit] Other than dissecting Fred to 3 individual variables and back
 
Last edited:
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
The problem , not a bug, is as mentioned in the first post in that thread you linked. It is not a bug because objects are passed by reference.
I want to save some of the Freds from an array of them at the point that they are going to change, let the originals change without changing the 'backup copy' and then restore the 'backup copy' to the original for an Undo. Quickly not using a file system or database which is overkill.

It may well be possible to get around it with complicated coding etc., however I do note that by splitting the Type at the point of saving/copying, into its constituent parts and using normal variables and then reconstituting the Type when using the Undo, will work, but sort of defeats the attaching of the type to an array.

I find it difficult to believe that there is not a simple solution (CopyByValue) or any other name you can think of that will dissociate the two copies i.e. by value not reference. There should be at least half a million people could use it.

I suppose I could write a Function to split the type and reconstitute it called a bodge.
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Thank you Erel, yes that is what I need but I was sidetracked by following your link and it indicated reliance on SQL and RandomAccessFile libraries and this line in your example...
kvs.Initialize(File.DirInternalCache, "datastore") which I must confess immediately threw me :signOops: as a massive overkill or so it seemed for keeping a few copies for a few minutes.
I apologise :sign0013: for not following through after that. I will now try properly and stop wasting everybody's time.
 
Upvote 0
Top