Computer Science Canada

Doesn't the Split() function return an array?

Author:  GlobeTrotter [ Sat Mar 25, 2006 4:24 pm ]
Post subject:  Doesn't the Split() function return an array?

Okay, so I have this small piece of code and I just don't understand the problem.

I have a WriteFile procedure that takes in a string array and writes it to a file. I am calling a split of a string so that it can go to the writefile:

code:

Call WriteFile(Split(strTempStringOutput, vbCrLf), strTempFileName)



I'm getting the error, however, "Type mismatch: Array of user-defined type expected"

I don't get it. The Split() fcn should return an array.
If it is of any help, here is the WriteFile procedure.

code:

Public Sub WriteFile(LineArray() As String, strFileName As String)
    Dim FileNum As Integer
    Dim lCount As Long
   
    FileNum = FreeFile
    Open strFileName For Output As FileNum
   
    If Not isStringArrayEmpty(LineArray) Then
        For lCount = 0 To UBound(LineArray)
            Print #FileNum, LineArray(lCount)
        Next lCount
    End If
    Close #FileNum
End Sub

Author:  Brightguy [ Sun Mar 26, 2006 2:01 am ]
Post subject:  Re: Doesn't the Split() function return an array?

Hmmm... I thought VB would automatically recognize that Split() returns a string array, but I guess it defaults to using the Variant type.

So either declare LineArray as a Variant, or specifically declare a string array, set it equal to what Split() returns, and pass that.

Author:  GlobeTrotter [ Sun Mar 26, 2006 2:30 pm ]
Post subject: 

Alright, I tried specifically declaring a string array, passing the split function to that, but it still isn't working. I'd like to avoid variants if possible.

code:

Public Function ConcatonateStringIntoArray(strInput As String) As String()
    Dim strArray() As String
    strArray = Split(strInput, vbCrLf)
    ConcatonateArrayIntoString = strArray
End Function


It says, function call on left side must return variant or object...

Author:  Brightguy [ Sun Mar 26, 2006 10:14 pm ]
Post subject:  Re: Doesn't the Split() function return an array?

There's no need to create a new function, although that seems to work in VB6. (You can't return arrays in VB5 unless it's a Variant containing an array.)

The "Function call on left side of assignment must return Variant or Object" could occur if you have two functions defined - "ConcatonateArrayIntoString" and "ConcatonateStringIntoArray". Otherwise you should be getting a "Variable not defined" error - you are using Option Explicit, aren't you?

Author:  GlobeTrotter [ Sun Mar 26, 2006 10:30 pm ]
Post subject: 

Wow, I feel like an idiot. I do have two functions, one string to arrya and vice versa for the other. Not sure why I used the frong function name to return the result. Works great now, thanks.


: