canalrun Well-Known Member Licensed User Longtime User Jan 26, 2015 #1 Hello, I would like to create a second instance of a type variable and then copy the contents of of the first instance of that type variable to the second. I don't want to copy a reference to the first instance, I want to actually create a new variable instance that contains the same data. Erel has and example in B4A using KeyValueStore. Is this available in B4J? For example: Code: Copy Type MyPoint (l, t as Int) Dim P1, P2 as MyPoint P1.Initialize P1.l = 5 P1.t = 10 P2.Initialize P2 = CopyTheDataFrom(P1) ' if I do P2 = P1, P2 gets a reference to P1 ' if I set P2.l = 7, P1.l would also be 7 Is there a way to do this other than writing a special function CopyTheDataFrom? Thanks, Barry.
Hello, I would like to create a second instance of a type variable and then copy the contents of of the first instance of that type variable to the second. I don't want to copy a reference to the first instance, I want to actually create a new variable instance that contains the same data. Erel has and example in B4A using KeyValueStore. Is this available in B4J? For example: Code: Copy Type MyPoint (l, t as Int) Dim P1, P2 as MyPoint P1.Initialize P1.l = 5 P1.t = 10 P2.Initialize P2 = CopyTheDataFrom(P1) ' if I do P2 = P1, P2 gets a reference to P1 ' if I set P2.l = 7, P1.l would also be 7 Is there a way to do this other than writing a special function CopyTheDataFrom? Thanks, Barry.
stevel05 Expert Licensed User Longtime User Jan 26, 2015 #2 No, you have to clone each element at it's lowest level. Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Jan 26, 2015 #3 Yes. You can use KeyValueStore in B4J as well to clone an object: KeyValueStore Upvote 0
stevel05 Expert Licensed User Longtime User Jan 26, 2015 #4 Very true, I have used it to clone and backup classes. For the example given it would be simpler to write a small subroutine. Upvote 0
Very true, I have used it to clone and backup classes. For the example given it would be simpler to write a small subroutine.
canalrun Well-Known Member Licensed User Longtime User Jan 26, 2015 #5 Thanks both. In a general sense, using KeyValueStore would probably be better, but for my simple case I went with the subroutine approach. Something like: Code: Copy Sub CopyTheDataFrom(p1 as MyPoint) as MyPoint dim p2 as MyPoint p2.Initialize p2.l = p1.l p2.t = p1.t return(p2) end sub Then the code P2 = CopyTheDataFrom(P1) produces a new instance of MyPoint copying the data. Barry. Upvote 0
Thanks both. In a general sense, using KeyValueStore would probably be better, but for my simple case I went with the subroutine approach. Something like: Code: Copy Sub CopyTheDataFrom(p1 as MyPoint) as MyPoint dim p2 as MyPoint p2.Initialize p2.l = p1.l p2.t = p1.t return(p2) end sub Then the code P2 = CopyTheDataFrom(P1) produces a new instance of MyPoint copying the data. Barry.