Wish: Clone and ToArray methods for structures

corwin42

Expert
Licensed User
Longtime User
I would love to have a builtin Clone method for structures.

This code example explains it all:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Type struc (a As String, b As String)
   Dim s1, s2 As struc
End Sub

Sub Activity_Create(FirstTime As Boolean)
   s1.Initialize
   s1.a = "One"
   s1.b = "Two"

   ' clone s1 object
   s2 = s1.Clone
   
   ' Result: One Two
   Msgbox(s2.a & " " & s2.b, "Output")
   
   ' copy reference to object (this works now)
   s2 = s1
   
   s2.a = "Three"
   
   'Result: Three Two
   Msgbox(s1.a & " " & s1.b, "Output")
End Sub

Also a ToArray method would be nice. I use a general Sub for inserting structure data into a database. Currently I have to Write the following to insert a row:

B4X:
Database.InsertTableData(sq, "BatteryData", Array As Object(BD.Ticks, BD.Duration, BD.Level, BD.Status, BD.Plugged, BD.ScreenOn))

With a ToArray method this will shorten my code to the following line:

B4X:
Database.InsertTableData(sq, "BatteryData", BD.ToArray)
 

JesseW

Active Member
Licensed User
Longtime User
How about views?

RandomAccessFile doesn't include views as supported types. Is there a way to clone a view?

My situation is I need 48 labels to display info as a grid. There are six columns. I'd rather not define them all in the designer. But if I could define one, I could load the layout and clone the rest without creating all six columns in code inside a loop.

Thanx...

Edit: after some more thought, I realized another way I could achieve what I wanted without too much duplicate coding. But it would still be nice to have the ability to clone an object. Thanks Erel for a superb dev environment :)
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Cloning objects is much more complicated than it may sound. An object can hold references to many other objects. Do you want to clone them too (deep copy vs. shallow copy)?
If you completely clone a view you will end up with two activities.
Other solutions are much simpler and manageable: Basic4android Search: arrays of views
 
Top