R rafaelmotaquintana Active Member Licensed User Apr 6, 2016 #1 I have this type Code: 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?
I have this type Code: 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 Apr 6, 2016 #2 Code: 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
Code: 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
SteveTerrell Active Member Licensed User Longtime User Apr 6, 2016 #3 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
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
johndb Active Member Licensed User Longtime User Apr 6, 2016 #4 SteveTerrell said: 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 Click to expand... 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: Apr 6, 2016 Upvote 0
SteveTerrell said: 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 Click to expand... 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
Erel B4X founder Staff member Licensed User Longtime User Apr 7, 2016 #6 Another option: Code: 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. Upvote 0
Another option: Code: 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.
johndb Active Member Licensed User Longtime User Apr 7, 2016 #7 Erel said: Another option: Code: 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. Click to expand... 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
Erel said: Another option: Code: 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. Click to expand... 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?
DonManfred Expert Licensed User Longtime User Apr 7, 2016 #8 To copy properties is not the same as cloning a class. The class still needs to be initialized and so on. Upvote 0
To copy properties is not the same as cloning a class. The class still needs to be initialized and so on.
Erel B4X founder Staff member Licensed User Longtime User Apr 7, 2016 #9 There is no single "best way". You can create a clone method that manually copies the data to a different class. Another option is to have no class variables except of a single custom type. You can then copy the custom type with B4XSerializator. Upvote 0
There is no single "best way". You can create a clone method that manually copies the data to a different class. Another option is to have no class variables except of a single custom type. You can then copy the custom type with B4XSerializator.
johndb Active Member Licensed User Longtime User Apr 7, 2016 #10 DonManfred said: To copy properties is not the same as cloning a class. The class still needs to be initialized and so on. Click to expand... 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
DonManfred said: To copy properties is not the same as cloning a class. The class still needs to be initialized and so on. Click to expand... 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.
SteveTerrell Active Member Licensed User Longtime User Apr 7, 2016 #11 A related interesting point... Code: 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
A related interesting point... Code: 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
DonManfred Expert Licensed User Longtime User Apr 7, 2016 #12 SteveTerrell said: presumably both sub1 and sub2 would receive the map (some time later) with "action" at 58? Click to expand... yes Code: 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 said: presumably both sub1 and sub2 would receive the map (some time later) with "action" at 58? Click to expand... yes Code: 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
SteveTerrell Active Member Licensed User Longtime User Apr 8, 2016 #13 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
Thanks I did miss out the numerous other "common" values to be put in the map I am sill suffering from C++ withdrawal! Steve