Results 1 to 3 of 3

Thread: StringArray: exactly like Array, but returns a string array

  1. #1

    Thread Starter
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    StringArray: exactly like Array, but returns a string array

    Code:
    Public Function StringArray(ParamArray Strings() As Variant) As String()
        Static strTemp() As String
        Dim lngUB As Long, lngA As Long
        lngUB = UBound(Strings)
        If lngUB > -1 Then
            ReDim Preserve strTemp(lngUB)
            For lngA = 0 To lngUB
                strTemp(lngA) = CStr(Strings(lngA))
            Next lngA
            StringArray = strTemp
        Else
            StringArray = Split(vbNullString)
        End If
    End Function
    Sample usage:
    Code:
    Private Sub Form_Load()
        MsgBox Join(StringArray("Test", "Really."), " this thing. ")
    End Sub
    Notes:
    The function always returns an existing array. UBound may be -1 if there are no items in the array.

    This function doesn't have error detection, this means you could pass objects, controls etc. into the function, which would result into an error message (instead of the function silently handling the error and resulting into a null string).


    (I actually found it interesting that I didn't find even an attempt to create this function anywhere on this forum.)

  2. #2

    Re: StringArray: exactly like Array, but returns a string array

    Wouldn't it be better written like this?

    Code:
    Public Function StringArray(ParamArray Strings() As Variant) As String
            StringArray = Strings
    End Function

  3. #3

    Thread Starter
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: StringArray: exactly like Array, but returns a string array

    For the simple reason it doesn't work: you can't place a Variant array into String array, so you're forced to create your own array and loop through each item and convert them to strings, then return that.

    If placing Variant array into a String array would work, then there wouldn't be need for StringArray at all, because just using Array would do the same (it makes a Variant array).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width