Dave O Well-Known Member Licensed User Longtime User Nov 25, 2011 #1 Hi there, Here's a pretty basic question about objects for the OO types out there: How do I copy an object to another object? For example, I'd like to copy object A to object B, but still maintain them as separate data. As I understand it, when I do this with the two objects: B = A ...B now points to the same object as A (i.e. they're both references to the same data instance). What I want is to *copy* A to B, then be able to change B without affecting A - like what would happen if I was dealing with primitive variables. Is there an easy way to do this, or do I have to write a copy function for each type of object I'm working with? Thanks for any help!
Hi there, Here's a pretty basic question about objects for the OO types out there: How do I copy an object to another object? For example, I'd like to copy object A to object B, but still maintain them as separate data. As I understand it, when I do this with the two objects: B = A ...B now points to the same object as A (i.e. they're both references to the same data instance). What I want is to *copy* A to B, then be able to change B without affecting A - like what would happen if I was dealing with primitive variables. Is there an easy way to do this, or do I have to write a copy function for each type of object I'm working with? Thanks for any help!
Erel B4X founder Staff member Licensed User Longtime User Nov 25, 2011 #2 You can use RandomAccessFile.WriteObject / ReadObject: (untested code) Code: Copy Sub CopyObject(Obj As Object) As Object Dim raf As RandomAccessFile raf.Initialize(File.DirInternalCache, "1.dat", false) raf.WriteObject(Obj, True, 0) Dim newObj As Object newObj = raf.ReadObject(0) raf.Close Return newObj End Sub You can keep the temp file open and make raf variable a global variable. Upvote 0
You can use RandomAccessFile.WriteObject / ReadObject: (untested code) Code: Copy Sub CopyObject(Obj As Object) As Object Dim raf As RandomAccessFile raf.Initialize(File.DirInternalCache, "1.dat", false) raf.WriteObject(Obj, True, 0) Dim newObj As Object newObj = raf.ReadObject(0) raf.Close Return newObj End Sub You can keep the temp file open and make raf variable a global variable.
Vader Well-Known Member Licensed User Longtime User Feb 2, 2013 #3 Fyi in the .net world, this is referred to as a clone. B=A.Clone Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Feb 3, 2013 #4 .Net Clone is pretty unusable as it doesn't determine whether it is a shallow clone or a deep clone (like done here). You can read more about it here: Should we Obsolete ICloneable (The SLAR on System.ICloneable) - Brad Abrams - Site Home - MSDN Blogs Upvote 0
.Net Clone is pretty unusable as it doesn't determine whether it is a shallow clone or a deep clone (like done here). You can read more about it here: Should we Obsolete ICloneable (The SLAR on System.ICloneable) - Brad Abrams - Site Home - MSDN Blogs
Vader Well-Known Member Licensed User Longtime User Feb 3, 2013 #5 Erel said: .Net Clone is pretty unusable as it doesn't determine whether it is a shallow clone or a deep clone (like done here). You can read more about it here: Should we Obsolete ICloneable (The SLAR on System.ICloneable) - Brad Abrams - Site Home - MSDN Blogs Click to expand... Yep. I usually serialize the object down to a text file, then build it back up. Pretty well what I think you do. edit: When I say "down" I mean all the way to object(). Last edited: Feb 3, 2013 Upvote 0
Erel said: .Net Clone is pretty unusable as it doesn't determine whether it is a shallow clone or a deep clone (like done here). You can read more about it here: Should we Obsolete ICloneable (The SLAR on System.ICloneable) - Brad Abrams - Site Home - MSDN Blogs Click to expand... Yep. I usually serialize the object down to a text file, then build it back up. Pretty well what I think you do. edit: When I say "down" I mean all the way to object().
DavideV Active Member Licensed User Longtime User Oct 5, 2015 #6 Erel said: You can use RandomAccessFile.WriteObject / ReadObject: (untested code) Code: Copy Sub CopyObject(Obj As Object) As Object Dim raf As RandomAccessFile raf.Initialize(File.DirInternalCache, "1.dat", false) raf.WriteObject(Obj, True, 0) Dim newObj As Object newObj = raf.ReadObject(0) raf.Close Return newObj End Sub You can keep the temp file open and make raf variable a global variable. Click to expand... Thx Erel you saved my time Upvote 0
Erel said: You can use RandomAccessFile.WriteObject / ReadObject: (untested code) Code: Copy Sub CopyObject(Obj As Object) As Object Dim raf As RandomAccessFile raf.Initialize(File.DirInternalCache, "1.dat", false) raf.WriteObject(Obj, True, 0) Dim newObj As Object newObj = raf.ReadObject(0) raf.Close Return newObj End Sub You can keep the temp file open and make raf variable a global variable. Click to expand... Thx Erel you saved my time
wonder Expert Licensed User Longtime User Mar 29, 2016 #7 Hi! Is this, to date, the best way to copy an object? I'd like to copy an array of objects, say 512. Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Mar 30, 2016 #8 It is better to use B4XSerializator: Code: Copy Sub CopyObject(o As Object) As Object Dim s As B4XSerializator Return s.ConvertBytesToObject(s.ConvertObjectToBytes(o)) End Sub Note that not all types are supported. Supported types are: primitives, strings, lists, maps, arrays of objects or bytes, custom types and any combination of those (a map that holds arrays of custom types). Upvote 0
It is better to use B4XSerializator: Code: Copy Sub CopyObject(o As Object) As Object Dim s As B4XSerializator Return s.ConvertBytesToObject(s.ConvertObjectToBytes(o)) End Sub Note that not all types are supported. Supported types are: primitives, strings, lists, maps, arrays of objects or bytes, custom types and any combination of those (a map that holds arrays of custom types).