Make object unique

sterlingy

Active Member
Licensed User
Longtime User
I have two objects of the same type

B4X:
Private tempBMPData, setupBMPData As BitmapData

Then I want to make one equal to the other

B4X:
tempBMPData = setupBMPData

The problem is that when I CLEAR setupBMPData, it also clears tempBMPData. I don't want both variables pointing to the same object. How do I copy the contents of one variable to the other, but keep them pointing to separate objects?

-Sterling
 

agraham

Expert
Licensed User
Longtime User
You would have to copy the individual items
B4X:
Dim tempBMPData, setupBMPData As BitmapData
...
tempBMPData.Bitmap = setupBMPData.Bitmap
tempBMPData.SrcRect = setupBMPData.SrcRect
tempBMPData.DestRect = setupBMPData.DestRect
tempBMPData.Rotate = setupBMPData.Rotate
tempBMPData.Flip= setupBMPData.Flip
You now have two instances of BitmapData but note that they share three reference objects. Bitmap, SrcRect and DestRest. This may or may not be what you want. If you want a truly independent copy you would need to clone those objects. I haven't tested the following but it looks OK at first sight.
B4X:
Dim tempBMPData, setupBMPData As BitmapData
...
Dim newSrcRect, newDestRect As Rect
Dim newBitmap As Bitmap

newBitmap.Initialize(setupBMPData.Bitmap)
tempBMPData.Bitmap = newBitmap
newSrcRect.Initialize(setupBMPData.SrcRect.Left, setupBMPData.SrcRect.Top, setupBMPData.SrcRect.Right, setupBMPData.SrcRect.Bottom) 
tempBMPData.SrcRect = newSrcRect

newDestRect.Initialize(setupBMPData.DestRect.Left, setupBMPData.DestRect.Top, setupBMPData.DestRect.Right, setupBMPData.DestRect.Bottom) 
tempBMPData.DestRect = newDestRect

tempBMPData.Rotate = setupBMPData.Rotate
tempBMPData.Flip= setupBMPData.Flip
You might want to ask yourself if you really need a deep copy like this or if you can avoid having to do it.
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
The first option would probably do it, but I should have been clearer. I'm really trying to clone a GameView, in which the BitmapsData is a LIST. I'll try to figure it out. If you have some ideas, I'd love to see 'em.

-Sterling
 
Upvote 0
Top