BASIC: Error 424 when trying to copy a user-defined data type with an array

I’m trying to use ReDim Preserve to copy the contents of an array within a user-defined type but I am getting an Object Required error (error number 424) at the line just before the ReDim in the following (as commented):

type _t1
   i as integer
   s as string
end type

type _t2
   j as integer
   t() as _t1
end type

type _t3
   k as integer
   t2() as _t2
end type

private function copy_t3(src as _t3) as _t3

   dim copy as _t3
   dim t2a(1 to 5, 1 to 5) as _t2   ' _t2 array used to copy the array's data

   ' Copy t3-level data
   copy.k = src.k

   t2a = src.t2    ' <<<<  ERROR OCCURS HERE
   redim preserve t2a(1 to 5, 1 to 5) as _t2
   copy.t2 = t2a
end function

Sub Main

  dim x as _t3   
  dim y(1 to 5, 1 to 5) as _t2
  ' fill y
  for ii = 1 to 5
     for jj = 1 to 5
        y(ii,jj).j = ii * 10 + jj
        ' I'm ignoring the fact that _t2 contains an array for now...
     next jj
  next ii
  x.t2 = y
  dim cpy as _t3
  cpy = copy_t3(x)
End Sub

I’ve spent all afternoon trying all sorts of syntax variations and trying to google an answer to no avail. As a relative noob to LibO Basic, I think I’m missing something. Does anyone have any ideas as to what? :slight_smile: Or, if you have an example solution to the general problem of copying a user-defined data type, that’d be helpful too.

And to be clear, I’m looking for a duplicate variable, not just a pointer to the same copy of data. I want to change the data in cpy without changing the data in x.

Any pointers would be greatly appreciated! Thanks!!


Years ago I created a set of array tools in Basic mainly for myself, but I rarely used it since, and I also did not yet study the tools by ScriptForge (LibreOffice 7.1 Community: Release Notes - The Document Foundation Wiki) available in LibO V7.1 and higher which may provide the needed functionality, and will be much more professional.
You may open the attached container-only .ods and try my tools with a few changes in your code.
Disclaimer: I would expect problems with the result because the field copy.t2 has a different type (Object —> Array) when returned by createArrayCopy(). You need to check everything yourself.

@chucks12065: I would like to get informed about your progress.

@Lupp, Thanks for the reply. Your mentioning the type difference got me past the issue, although mostly by brute force. I guess I was under the impression that an array was a type of object and could be assigned to an object. Guess not. Thanks again!