B4J Question Can a clone of a class instance be created in B4J?

nbarakat

Member
Hello All,

I am wondering if it is possible to create a clone of a class instance without having to explicitly equate the properties of one instance to another.
I tried B4XSerialization and kept getting errors; Then I read that it cann't be used on class instances.

Somethink Like below

B4X:
Dim cl1 as Class1
Dim cl2 as Class1
cl1.P1="Hello"
....
cl2=Clone(cl1)
cl2.P1="Hi"
Log(cl1.P1 ==> "Hello")
Log(cl2.P1 ==> "Hi")

I know it can be done by equating the properties but for big classes with list of classes this would be tedious also everytime you would change your class you need to modify your cloning ...

Thanks
 

nbarakat

Member
Thank Erel for your reply. Iam seeking a more generic solution to this. Can you point me in the right direction of building a generic routine for this? Basically I would need to:

1. Read the class structure (Mainly properties)
2. Access the values of the instance based on the retrieved structure
3. Create a new instance and save the accessed values to it

Thanks for your support.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
1. Read the class structure (Mainly properties)
2. Access the values of the instance based on the retrieved structure
3. Create a new instance and save the accessed values to it
You can only do that for arbitrary class instances in Java code using reflection, which is, no offence meant, probably a bit esoteric for you to attempt. I would use Erel's Clone method suggestion if it were me.
 
Upvote 0

nbarakat

Member
Thanks Erel for the feedback and agraham for the pointer and suggestion. For now I will use Erels method until I have more time and knowledge to look into Java reflection API.:rolleyes:
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I did something similar to what you are asking
B4X:
Public  Sub CloneCopy(CloneMe As Object) As Object
            If  CloneMe Is page2 Then
                Dim CloneMe2   as page2 = CloneMe
                Dim ClonePage2 As page2
              
                ClonePage2.Initialize               
                ClonePage2.<some variable> = CloneMe2.<some variable>
                '  Do whatever the class cloning needs
              
                Return ClonePage2
            End If
          
            If  CloneMe Is page3 Then
                Dim CloneMe3   as page3 = CloneMe
                Dim ClonePage3 As page3
              
                ClonePage3.Initialize
                ClonePage3.<some variable> = CloneMe3.<some variable>
                '  Do whatever the class cloning needs
              
                Return ClonePage3              
            End If
          
            Return Null
End Sub

This way I had a routine that would clone any (in this case class - in my real case I was cloning type structures)

Attached a file that shows the routine (was part of my grid class)


Not sure if this helps

BobVal
 

Attachments

  • GidCopy.txt
    4.5 KB · Views: 93
Last edited:
Upvote 0

nbarakat

Member
Thanks Bob for your input. I ended up doing something similar to what you and Erel suggested.

I guess I was seeking a more generic solution where one doesn't know the properties and events of the passed class to be cloned (Passing the class by Value instead of by reference). In order to do this in B4J I believe as agraham suggested one needs to resort to doing it in java using reflection ...
 
Upvote 0
Top