A string array without specified dimensions

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
gutee
Posts: 19
Joined: Wed Jan 09, 2019 9:12 am

A string array without specified dimensions

Post by gutee »

I need to define one Array, the element inside will be String, but I do not know how many elements at the beginning.
my code is :
Dim my_str_arr() As String
my_str_arr(1) = "Test_Str"
It will show error : Inadmissible value or data type. Index out of defined range.

It works if I define the Array with dimensions defined.
Dim my_str_arr(2) As String
my_str_arr(1) = "Test_Str"
OpenOffice.org.3.3.0
FJCC
Moderator
Posts: 9273
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: A string array without specified dimensions

Post by FJCC »

You can use
ReDim Preserve
to change the size of an array while preserving the content. Please see the Help section for information about that command.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
robleyd
Moderator
Posts: 5080
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: A string array without specified dimensions

Post by robleyd »

See https://wiki.openoffice.org/wiki/Docume ... ide/Arrays for ways of varying the number of elements in an array via REDIM, or see the section on empty arrays. Note that page also says "There are no practical limits on the indexes or on the number of elements in an array, so long as there is enough memory".

I suggest you read all of the page as you are likely to have other array questions which can be answered by reference to that page.
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: A string array without specified dimensions

Post by JeJe »

One Caution... because of some minor annoying faults with OO it can be better to declare my_str_arr() as Variant instead of string. If you're going to use the split function for example...

viewtopic.php?f=45&t=96665
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply