Android Question Copy 'Type' type variable to another

rafaelmotaquintana

Active Member
Licensed User
I have this type

B4X:
  Type CustRECORDTYPE( _
     ID As Int, _
     CODE As String, _
     NAME As String
     )
   Dim CustRECORD As CustRECORDTYPE
   Dim CustRECORD2 As CustRECORDTYPE

Later, I use CustRECORD2 = CustRECORD
And I see that whenever I change CustRECORD, also CustRECORD2 is changed. It seems that it was the reference that was passed to one variable to another. Is there a way to just made a independent copy?
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim tt As CustRECORDTYPE
    tt.Initialize
    tt.ID = 1
    tt.CODE = "c"
    tt.NAME = "n"
  
    Dim tt2 As CustRECORDTYPE
    tt2 = copytype(tt)
    Log(tt2)



Sub copytype(src As CustRECORDTYPE) As CustRECORDTYPE
    Dim dst As CustRECORDTYPE
    dst.Initialize
    dst.ID = src.ID
    dst.CODE = src.CODE
    dst.NAME = src.NAME
    Return dst
End Sub
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
Is it the case that it is a reference until something is changed in the variable receiving the copy?

i.e. after tt2=tt executing tt2.ID = 57 results in a new copy of the Type which is now different from tt or is it that
both tt.ID and tt2.ID are now 57

Regards
Steve
 
Upvote 0

johndb

Active Member
Licensed User
Longtime User
Is it the case that it is a reference until something is changed in the variable receiving the copy?

i.e. after tt2=tt executing tt2.ID = 57 results in a new copy of the Type which is now different from tt or is it that
both tt.ID and tt2.ID are now 57

Regards
Steve
tt2 and tt will point to the same data so if you assign 57 to tt2.ID it would be the same as if you assigned 57 to tt.ID. The only way that you can have two distinct data sets is by creating a new data set and copy the contents as @DonManfred had previously stated. This same philosophy also applies to classes. I always create a 'Clone" method in all of my classes which will copy the data to a new class instance.

John
 
Last edited:
Upvote 0

johndb

Active Member
Licensed User
Longtime User
Another option:
B4X:
Sub CopyObject(o As Object) As Object
   Dim s As B4XSerializator
   Return s.ConvertBytesToObject(s.ConvertObjectToBytes(o))
End Sub

There are some limitations to the supported objects. However most custom types are supported.
Thank you @Erel. Since B4XSerializator doesn't support traditional classes, would the best way of handling class cloning be to create a custom type in the class to contain the class's data and use the CopyObject method to clone the data?
 
Upvote 0

johndb

Active Member
Licensed User
Longtime User
To copy properties is not the same as cloning a class.
The class still needs to be initialized and so on.
Yes, the class still has to be initialized however the CopyObject method does save coding time to copy one class property of a custom type instead of copying each individual discrete class property as I do now. ;)
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
A related interesting point...

B4X:
Sub SendMap
Dim m as Map
m.initialize
m.Put("action",57)
CallSubDelayed2(Main,"sub1",m)
m.Put("action",58)
CallSubDelayed2(Main,"sub2",m)
End Sub

presumably both sub1 and sub2 would receive the map (some time later) with "action" at 58?

Steve
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
presumably both sub1 and sub2 would receive the map (some time later) with "action" at 58?
yes
B4X:
Sub SendMap
Dim m as Map
m.initialize
m.Put("action",57)
CallSubDelayed2(Main,"sub1",m)
Dim m as Map
m.initialize
m.Put("action",58)
CallSubDelayed2(Main,"sub2",m)
sub1 will receive 57
sub2 58
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
Thanks
I did miss out the numerous other "common" values to be put in the map ;)
I am sill suffering from C++ withdrawal!
Steve
 
Upvote 0
Top