[Wish] CopyObject(object) as object

Eduard

Active Member
Licensed User
Longtime User
I would like to be able to copy an entire object.

if I have two object, let's say a and b
dim a,b as someobject
a.initialize
a.property=1
b=a
b.property=2

msgbox(a.property,"") would of course return 2 because a and b are the same object

I would like to be able to copy the object

a.initialize
a.property=1
b=CopyObject(a)
b.property=2

so that msbox(a.property,"") would return 1 .

Don't know if its possible. But it would save time. I do need to modify a list or map from time to time and preserve the original.
 

Eduard

Active Member
Licensed User
Longtime User
:sign0142:

Thank you!
The first solution has the drawback that you must specify the buffer, and that if it's to small, you application will crash.

The second solution has the drawback that a file is created, that would slow things down a lot, and you'll need a unique filename in case of multithreading.

I created a third solution:
B4X:
Sub CopyObject(Obj As Object) As Object
   Dim raf As RandomAccessFile
   Dim n=256 as int
   Dim notdone=True As Boolean
   Do While notdone
      'Msgbox(n,"n")
      notdone=False
      Dim buffer(n) As Byte
      Try
         raf.Initialize3(buffer,False)
         raf.WriteObject(Obj, True, 0)
         Dim newObj As Object
         newObj = raf.ReadObject(0)
      Catch
         n=n*10
         notdone=True
      End Try
      raf.Close
   Loop
   Return newObj
End Sub
This will always work and is fast. I've tested it and it works.
 
Last edited:

EduardoElias

Well-Known Member
Licensed User
Longtime User
Hi there,

Please I am having the following problem:

I am using this CopyObject to copy a initiated class from a library created with compile to library.

I have tried other variants of copyobject. All are creating a null object.

Is it allowed to make a copy of user defined class object?

What gets copied? Any other references on class_globals even to other objects gets copied?


What I am trying to accomplish is to in the main activity dim the objects that will be used on the apk. Those are a selection from many objects that can be used in a project. I am not really sure of the engine in B4A, but seems to me that every library selected (I mean b4a created ones) are always imported to the apk. So if I have hundreds of libraries and need only a selection for a specific library I need to declare all to use a few. I was thinking on mark the ones needed for that project and dim the object, passing it as a way to declare that it is available, and by cloning it use the new object.

Those libraries are part of a framework created, when the object is created and loaded the object to the framework it is recognized and started to be used, it is pretty much like installing an app to android.

Thanks Eduardo
 

Eduard

Active Member
Licensed User
Longtime User
Better version of the sub above:
B4X:
Sub CopyObject(Obj As Object) As Object
   Dim raf As RandomAccessFile
   Dim n=256 As Int
   Dim notdone=True As Boolean
   Do While notdone
      notdone=False
      Dim buffer(n) As Byte
      Try
         raf.Initialize3(buffer,False)
         raf.WriteObject(Obj, True, 0)
      Catch
         n=n*10
         notdone=True
      End Try
      If notdone=False Then
         Dim newObj As Object
         newObj = raf.ReadObject(0)
      End If
      raf.Close
   Loop
   Return newObj
End Sub

Discovered that ReadObject fails if you try to copy for example a class instance (which is not possible). Now it gives the proper error instead of a loop until memory is full.
 

JoanRPM

Active Member
Licensed User
Longtime User
I test this code but, for me, it doesn't work.

I try to copy the properties of a label, but this code always returns a Null.
I try this:
Label2 = CopyObject(Label1)
Previously I defined and inicialized Label1 and 2.

Many thanks.
 
Top