(Word2003) string array UBound is set to -1 ???

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

(Word2003) string array UBound is set to -1 ???

Post by ChrisGreaves »

Code: Select all

Function MergeArrays(strResult() As String, strTributary() As String)
    Dim lngAr As Long
    For lngAr = LBound(strTributary) To UBound(strTributary)
        ReDim Preserve strResult(UBound(strResult) + 1)
        strResult(UBound(strResult)) = strTributary(lngAr)
    Next lngAr
End Function
Sub TESTMergeArrays()
    Dim strResult() As String
    Dim strTributary() As String
'    strResult = Split("alpha beta gamma", " ")
'    strTributary = Split("delta epsilon", " ")
'    Call MergeArrays(strResult, strTributary)
'    strResult = Split("alpha beta gamma", " ")
'    strTributary = Split("", " ")
'    Call MergeArrays(strResult, strTributary)
'    strResult = Split("", " ")
'    strTributary = Split("delta epsilon", " ")
'    Call MergeArrays(strResult, strTributary)
    strResult = Split("", " ")
    strTributary = Split("", " ")
    Call MergeArrays(strResult, strTributary)
End Sub
Untitled.png
I am new to the Split() function, but am not new to string arrays.
I can't remember seeing a VBA string array with a negative upper-bound.
Is this a unique feature of the Split() function? :scratch:

The -1 UBound does not appear to be related to the parameter "limit: Optional. Number of substrings to be returned; –1 indicates that all substrings are returned "

Thanks
Chris
You do not have the required permissions to view the files attached to this post.
An expensive day out: Wallet and Grimace

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: (Word2003) string array UBound is set to -1 ???

Post by HansV »

As far as I know this is specific to the Split function - it gives the programmer a way to check that the array is empty without relying on an error handler.
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: (Word2003) string array UBound is set to -1 ???

Post by ChrisGreaves »

HansV wrote:
16 Jun 2021, 21:13
As far as I know this is specific to the Split function - it gives the programmer a way to check that the array is empty without relying on an error handler.
Thanks for the confirmation Hans.

Makes me wonder why so many aspects of VBA could be used properly only with the use of an error-handler.
When I feel that I have mastered Word2003, I shall get the next version ... :evilgrin:
Cheers
Chris
An expensive day out: Wallet and Grimace

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: (Word2003) string array UBound is set to -1 ???

Post by rory »

Filter can also return an array with upper bound -1
Regards,
Rory

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: (Word2003) string array UBound is set to -1 ???

Post by ChrisGreaves »

rory wrote:
17 Jun 2021, 13:02
Filter can also return an array with upper bound -1

Code: Select all

Sub test()
    Dim strAr() As String
    strAr = Split("alpha beta gamma delta epsilon", " ")
    Dim strBr() As String
    strBr = Filter(strAr, "e")
    Debug.Print UBound(strBr)
    strBr = Filter(strAr, "")
    Debug.Print UBound(strBr)
    strBr = Filter(strAr, "z")
    Debug.Print UBound(strBr)
End Sub
Can and Does!
Thank you Rory; so now we know that Split(), Join(), and Filter() were written by one team member at Microsoft :evilgrin:

Initially I was surprised that Filter("") returned all elements, but then that made sense to me.

Code: Select all

Sub test()
    Dim strAr() As String
    strAr = Split("alpha beta gamma delta epsilon", " ")
    Dim strBr() As String
    strBr = Filter(strAr, "e")
    Debug.Print LBound(strBr)
    strBr = Filter(strAr, "")
    Debug.Print LBound(strBr)
    strBr = Filter(strAr, "z")
    Debug.Print LBound(strBr)
End Sub
For bonus marks, are these the only times that the LBound of an array can be greater than the UBound of the array? :evilgrin: :evilgrin:
And if so, how can I put this knowledge to good use? :evilgrin: :evilgrin: :evilgrin: :evilgrin: :evilgrin:

Addendum: Now that array UPPER bounds can be negative, can negative strings be far behind? It is, after all, all about the data descriptors.


Cheers
Chris
An expensive day out: Wallet and Grimace