Page 1 of 1

A string array without specified dimensions

PostPosted: Tue Jan 22, 2019 5:16 am
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"

Re: A string array without specified dimensions

PostPosted: Tue Jan 22, 2019 6:20 am
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.

Re: A string array without specified dimensions

PostPosted: Tue Jan 22, 2019 6:36 am
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.

Re: A string array without specified dimensions

PostPosted: Tue Jan 22, 2019 11:08 am
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