|
-
Apr 18th, 2007, 07:56 AM
#1
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.)
-
May 21st, 2007, 03:50 AM
#2
Member
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
-
May 21st, 2007, 06:34 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|