Main Menu

copying typed arrays() question

Started by stevmjon, February 27, 2010, 10:18:08 PM

Previous topic - Next topic

stevmjon

hi

just checking something to make sure i have done this correctly.
what i want to do is to copy a typed array pos, into another pos, within the same array.

method 1   *did not wok
arrayt(2)=arrayt(5)
freecell arrayt(),5
***this method cloned the pointer, then zeroed pos 2 when i freed pos 5***
***not sure what 'explicitly name type fields' is. this is what the help file says to do. so i experimented in method 2***

method 2   *did work
arrayt(2).me1=arrayt(5).me1
arrayt(2).me2=arrayt(5).me2
arrayt(2).me3=arrayt(5).me3
freecell arrayt(),5
***this method worked perfectly

i am just checking to make sure i have done this correctly.

   thanks  stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

#1
    In method #1, you're not copying the 'data structure' you're copying the handle of the data structure.   So those two cells in the array, are now using/pointer at the same data..   If you set a field on either cell, the other will show this.  Also,  If you delete one of those, then the other cell is now invalid.  


PlayBASIC Code: [Select]
   Type tObject
Name$
X#,y#
EndType


Dim Stuff(10) as TObject


// Allocate a new 'tObject' structure/bank and assign it to the array @ cell 1
//
// This _overwrites_ whatever was in this cell.
//
// So If something already existed in this cell, it'll get _released_.
// Then the new structure/bank handle is written into the cell

Stuff(1) = New TObject
Stuff(1).Name$ ="Joe"
Stuff(1).x# =100
Stuff(1).Y# =101

// Display the 'handle' of this cell
print Stuff(1)


// Assign cell #5 the bank hanles from cell #1
Stuff(5)=Stuff(1)

// Display the contents of the cells 5
// notice how they're the same.. That's because, we're not making a copy
// the 'tobject' structure, merely copying the bank handle value between cells.
// so cell 5 is not a copy of the other cell, they're in fact pointer at the/using
// the same data.
print Stuff(5)


// We can show they're the same data as if we change one of them, the other
// we reflext that change.
Stuff(5).Name$="Dude"

print ""
print "Display the Names of Cell 1 and Cell5 after copy"
print STuff(5).name$
print STuff(1).name$



// Since cell 5 and 1 are point at the same data, if i delete one of them
// the other is doomed
print ""
print "results before delete"

print Stuff(1).name$
print Stuff(1).x#
print Stuff(1).y#

print Stuff(5).name$
print Stuff(5).x#
print Stuff(5).y#

Stuff(5) =0

print "results after delete"
print Stuff(1).name$
print Stuff(1).x#
print Stuff(1).y#

print Stuff(5).name$
print Stuff(5).x#
print Stuff(5).y#




Sync
Waitkey






    Copy A Typed Cell Safely using  'CopyArrayCells'

PlayBASIC Code: [Select]
   Type tObject
Name$
X#,y#
EndType

Dim Stuff(10) as TObject

// Copy A Typed Cell Safely using 'CopyArrayCells

// Init this cell
Stuff(1) = New TObject
Stuff(1).Name$ ="Joe"
Stuff(1).x# =100
Stuff(1).Y# =101


// Display the 'handles' of the cells
Print "Handles Before Copy"
print Stuff(1)
print Stuff(5)


// Copy it to the same array but at position 5
CopyArrayCells Stuff(),1,1,Stuff(),5,1,1


// Display the 'handles' of the cells
Print "Handles After Copy"
print Stuff(1)
print Stuff(5)



// Since cell 5 and 1 are point at the same data, if i delete one of them
// the other is doomed
print ""
print "Display the Contents Of the Cells"

PrintCell(1)
PrintCell(5)

// Kill Cell 5
Stuff(5) =0

print "results after delete"
PrintCell(1)
PrintCell(5)




Sync
Waitkey


Psub PrintCell(index)
ink $00ff00
print ""
print "Name="+Stuff(Index).name$
print "X="+str$(Stuff(Index).x#)
print "Y="+str$(Stuff(Index).y#)
ink $ffffff
EndPsub




  Copy Between types via Pointer

PlayBASIC Code: [Select]
   Type tObject
Name$
X#,y#
EndType

Dim Stuff(10) as TObject

// Copy between cells via pointer

// Init this cell
Stuff(1) = New TObject
Stuff(1).Name$ ="Joe"
Stuff(1).x# =100
Stuff(1).Y# =101


// Display the 'handles' of the cells
Print "Handles Before Copy"
print Stuff(1)
print Stuff(5)


// Copy structures between two cells. if the destination cell
// does exist it'll be created prior to the copy.
//
Stuff(5).tObject =Stuff(1).Tobject

// Display the 'handles' of the cells
Print "Handles After Copy"
print Stuff(1)
print Stuff(5)


// Since cell 5 and 1 are point at the same data, if i delete one of them
// the other is doomed
print ""
print "Display the Contents Of the Cells"

PrintCell(1)
PrintCell(5)

// Kill Cell 5
Stuff(5) =0

print "results after delete"
PrintCell(1)
PrintCell(5)

Sync
Waitkey


Psub PrintCell(index)
ink $00ff00
print ""
print "Name="+Stuff(Index).name$
print "X="+str$(Stuff(Index).x#)
print "Y="+str$(Stuff(Index).y#)
ink $ffffff
EndPsub





   Copying Nested Type Fields Between Types

PlayBASIC Code: [Select]
   Type tPos
X#,y#
EndType

Type tObject
Name$
Pos as Tpos
EndType

Dim Stuff(10) as TObject

// Copy Nested TYPE structures.

// Init this cell
Stuff(1) = New TObject
Stuff(1).Name$ ="Joe"
Stuff(1).pos.x# =100
Stuff(1).pos.Y# =101


// Display the 'handles' of the cells
Print "Handles Before Copy"
print Stuff(1)
print Stuff(5)


// Copy structures between two cells. if the destination cell
// does exist it'll be created prior to the copy.
//

Stuff(5)=New TObject
Stuff(5).name$="Bill"

// Copy JOE's position into BILL
Stuff(5).pos =Stuff(1).pos

// Display the 'handles' of the cells
Print "Handles After Copy"
print Stuff(1)
print Stuff(5)


// Since cell 5 and 1 are point at the same data, if i delete one of them
// the other is doomed
print ""
print "Display the Contents Of the Cells"

PrintCell(1)
PrintCell(5)

// Kill Cell 5
Stuff(5) =0

print "results after delete"
PrintCell(1)
PrintCell(5)

Sync
Waitkey


Psub PrintCell(index)
ink $00ff00
print ""
print "Name="+Stuff(Index).name$
print "X="+str$(Stuff(Index).pos.x#)
print "Y="+str$(Stuff(Index).pos.y#)
ink $ffffff
EndPsub





Also See,

  Swapping Typed Cells In Arrays




stevmjon

to kev

thanks for your quick and informative input. only one problem.

> from your 4 examples posted above, i can't get example 3 (pointer) AND example 4 (nested) to work.

i was having trouble experimenting with copying types lately. i thought it simply didn't work the way i was using it, as i am still learning about them.
i have been using v1.64k2 .

i thought i was doing something wrong, but maybe this is a bug? can you check in case it is just my computer.
if it is a bug, i will post it in bugs with an example.

  stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

#3
 They all work fine here,  but the only version i use is the current WIP beta..

What you're experiencing is prolly this (login required)

kevin

#4
 Fixed in,

 PlayBasic V1.64L Beta3 (login required)

  PlayBasicFX V1.77  BETA #1 (login required)



stevmjon

thanks kev

works perfectly on the latest beta.

now i understand copying types, that it actually works...lol
i had to pick v1.64k2 to learn types, didn't i.

  stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.