[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libreoffice-users] 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: users+unsubscribe@global.libreoffice.org
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

Follow-Ups:
[libreoffice-users] Re: Basic Macro – Copy array of custom typeJohnny Rosenberg <gurus.knugum@gmail.com>
Privacy Policy | Impressum (Legal Info) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Creative Commons Attribution-Share Alike 3.0 License. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License (MPLv2). "LibreOffice" and "The Document Foundation" are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy.