Basic Macro – Copy array of custom type

classic Classic list List threaded Threaded
2 messages Options
Johnny Rosenberg Johnny Rosenberg
Reply | Threaded
Open this post in threaded view
|

Basic Macro – Copy array of custom type

Hi!
As we know by now, in LibreOffice Basic we can not copy an array directly,
we will only get a new name for the same array. It's like assigning two
array pointers to the same array. The obvious way to copy an array is to do
it element by element in a loop, but there is a workaround. Here's an
example that works:




















*Option ExplicitSub Test1    Dim A(9) As Long    Dim i As Integer'
Initialise a:    For i=0 To 9        A(i)=i    Next i'   Copy A to B:
Dim B(9) As Long: B=A    ReDim Preserve B(9)'   Do some changes to B
without affecting A:    B(7)=A(3)    B(3)=A(7)*
*'   Display the results:*







*    Dim Message As String, Tab As String, NL As String, Spc As String
Tab=Chr(9): NL=Chr(13): Spc="  "    Message="i" & Tab & "A(i)" & Tab &
"B(i)" & NL    For i=0 To 9        Message=Message & i & Tab & Spc & A(i) &
Tab & Spc & B(i) & NL    Next i    MsgBox MessageEnd Sub*

So, the trick in this case is to first assign B to A, then running the
ReDim statement. After that we have two separate arrays.

However, I want to do this with a custom type, like so:



*Private Type NumberType    Value    As Long    Status   As BooleanEnd Type*















*Sub Test2    Dim A(9) As NumberType    Dim i As Integer'    Initialise
a.    For i=0 To 9        A(i).Value=i        A(i).Status=True    Next
i'    Copy A to B    Dim B(9) As NumberType: B=A ' ”Basic runtime error:
Object required.”    ReDim Preserve B(9)End Sub*

This will throw a runtime error at *B=A* at third line from the end: ”Basic
runtime error: Object required”. What am I missing? How to do this properly?


Kind regards

Johnny Rosenberg

--
To unsubscribe e-mail to: [hidden email]
Problems? https://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: https://wiki.documentfoundation.org/Netiquette
List archive: https://listarchives.libreoffice.org/global/users/
Privacy Policy: https://www.documentfoundation.org/privacy
Johnny Rosenberg Johnny Rosenberg
Reply | Threaded
Open this post in threaded view
|

Re: Basic Macro – Copy array of custom type

Den sön 14 apr. 2019 kl 21:53 skrev Johnny Rosenberg <[hidden email]
>:

> Hi!
> As we know by now, in LibreOffice Basic we can not copy an array directly,
> we will only get a new name for the same array. It's like assigning two
> array pointers to the same array. The obvious way to copy an array is to do
> it element by element in a loop, but there is a workaround. Here's an
> example that works:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> *Option ExplicitSub Test1    Dim A(9) As Long    Dim i As Integer'
> Initialise a:    For i=0 To 9        A(i)=i    Next i'   Copy A to B:
> Dim B(9) As Long: B=A    ReDim Preserve B(9)'   Do some changes to B
> without affecting A:    B(7)=A(3)    B(3)=A(7)*
> *'   Display the results:*
>
>
>
>
>
>
>
> *    Dim Message As String, Tab As String, NL As String, Spc As String
> Tab=Chr(9): NL=Chr(13): Spc="  "    Message="i" & Tab & "A(i)" & Tab &
> "B(i)" & NL    For i=0 To 9        Message=Message & i & Tab & Spc & A(i) &
> Tab & Spc & B(i) & NL    Next i    MsgBox MessageEnd Sub*
>
> So, the trick in this case is to first assign B to A, then running the
> ReDim statement. After that we have two separate arrays.
>
> However, I want to do this with a custom type, like so:
>
>
>
> *Private Type NumberType    Value    As Long    Status   As BooleanEnd
> Type*
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> *Sub Test2    Dim A(9) As NumberType    Dim i As Integer'    Initialise
> a.    For i=0 To 9        A(i).Value=i        A(i).Status=True    Next
> i'    Copy A to B    Dim B(9) As NumberType: B=A ' ”Basic runtime error:
> Object required.”    ReDim Preserve B(9)End Sub*
>
> This will throw a runtime error at *B=A* at third line from the end:
> ”Basic runtime error: Object required”. What am I missing? How to do this
> properly?
>
>
> Kind regards
>
> Johnny Rosenberg
>
>
OK, to get rid of the error the keyword ”Let” was required. So finally I
know what that's for…
This code doesn't give me any errors, but the results are not right:

*Option Explicit*



*Private Type NumberType    Value    As Long    Status   As BooleanEnd Type*

































*Private Sub Test2    Randomize    Dim A(9) As NumberType    Dim i As
Integer'   Initialise a.    For i=0 To 9        A(i).Value=i
A(i).Status=True    Next i'   Copy A to B    Dim B(9) As New NumberType:
Let B=A    ReDim Preserve B(9)'   Do some changes to B without affecting
A:    B(7).Value=A(3).Value    B(3).Value=A(7).Value
B(3).Status=False    B(7).Status=False    Dim Message As String, Tab As
String, NL As String: Tab=Chr(9): NL=Chr(13)    Dim Spc1 As String, Spc2 As
String: Spc1=" ": Spc2="  "    Message="i" & Tab & "A(i)" & Tab & Tab & Tab
& "B(i)" & NL    For i=0 To 9        Message=Message & i & Tab & Spc2 &
_                A(i).Value & Spc1 & _                A(i).Status & Tab &
Tab & Spc2 & _                B(i).Value & Spc1 & _
B(i).Status & NL    Next i    MsgBox MessageEnd Sub*

It seems like ReDim Preserve has no effect in this case. A and B are still
pointers to the same array, as it seems.
Suggestions? Except assigning all the values in loops, because I already
know that…


Kind regards

Johnny Rosenberg

--
To unsubscribe e-mail to: [hidden email]
Problems? https://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: https://wiki.documentfoundation.org/Netiquette
List archive: https://listarchives.libreoffice.org/global/users/
Privacy Policy: https://www.documentfoundation.org/privacy