View Full Version : copy array of UDT to another array in one step?
ReneMiner
02-11-2012, 00:12
A few things about variables that I want to know, especially Types/UDTs
Dim A(10) As String
Dim B(10) As String
' can I copy the complete Array in one step like
A = B
' or
A() = B() '?
' just use a pre-defined type for next example
Dim Color1 As TBGL_TRGB
Dim Color2 As TBGL_TRGB
' can I copy a whole Type - (even if it's a UDT) -
' just like this:
Color1 = Color2
' now it get's ambitious:
Dim ColorTableA(5,5) As TBGL_TRGB ' I'm sure it needs to have the
Dim ColorTableB(5,5) As TBGL_TRGB ' same Dimensions...
ColorTableA = ColorTableB ' is this allowed?
'can I copy the whole thing at once or is there another
' smart and easy way to copy the memory in one step?
ErosOlmi
02-11-2012, 00:53
Let's start from the simple one: case 2 and case 3
Case 2 is just straightforward: variable 1 = variable 2
UDT variables are those cases just a sequence of bytes
' just use a pre-defined type for next example
Dim Color1 As TBGL_TRGB
Dim Color2 As TBGL_TRGB
' can I copy a whole Type - (even if it's a UDT) -
' just like this:
Color1 = Color2
Case 3: here we use functions working on direct memory access.
So copy from second variable the whole memory area over the memory area occupied by the first variable
' now it get's ambitious:
Dim ColorTableA(5,5) As TBGL_TRGB ' I'm sure it needs to have the
Dim ColorTableB(5,5) As TBGL_TRGB ' same Dimensions...
'---Store at the memory location of ColorTableA the whole binary content of ColorTableB
Poke$ VarPtr(ColorTableA(1, 1)), Peek$(VarPtr(ColorTableB(1, 1)), SizeOf(ColorTableB))
Now to the one that at first seems the simpler but is not due to the nature of thinBasic dynamic strings
As said many times in this forum, thinBasic dynamic strings are OLE32 BSTR strings: http://oreilly.com/catalog/win32api/chapter/ch06.html
It means that arrays of strings are in reality arrays of pointers to dynamically allocated strings handled by thinBasic core engine.
If we copy A() = B() we are in reality copying an array of pointer over another array of pointers pointing to the same allocated strings and it as no really meaning.
So in this case you need to loop over all elements of the array B copying over each elements of array A
That said, maybe in future I will support this syntax letting thinBasic Core Engine take all the stuff to do the job
ReneMiner
02-11-2012, 01:04
so if A(10) and B(10) were not strings but longs it would work that way:
Poke$ VarPtr(A(0)), Peek$(VarPtr(B(0)), SizeOf(B(0))*10) ?
then I've understood. Thanks, you're a good teacher.
ErosOlmi
02-11-2012, 01:15
Yes.
More simple way, SizeOf can automatically calculate the whole size of the array:
Poke$ VarPtr(A(1)), Peek$(VarPtr(B(1)), SizeOf(B))
PS: remember array index in thinBasic starts from 1 and not 0
More, if the array would be an array of fixed strings, it would work the same because fixed strings are not dynamic and an array of fixed strings is not an array of pointers but a sequence of bytes.
So this is valid:
DIM A(10) AS STRING * 255
DIM B(10) AS STRING * 255
Poke$ VarPtr(A(1)), Peek$(VarPtr(B(1)), SizeOf(B))
Fixed size strings are just sequence of bytes in memory, so are arrays of fixed strings.
Dynamic strings are instead always pointers to a dynamic allocated area internally handled by thinBasic Core Engine, allocated and de-allocated every time a dinamic string is changed.