Function returning an array - "Type mismatch" prob

Results 1 to 2 of 2

Thread: Function returning an array - "Type mismatch" prob

  1. #1
    Join Date
    Dec 1969

    Default Function returning an array - "Type mismatch" prob

    Hello,<BR><BR>I have a function:<BR><BR>Function GetParentPath(containerID)<BR> Dim LocalArray(2)<BR> Dim objRS_GPP, strSQL_GPP<BR> Set objRS_GPP = Server.CreateObject("ADODB.Recordset")<BR> strSQL_GPP = "SELECT name, parentID " & _<BR> FROM table WHERE ID = " & containerID<BR> objRS_GPP.Open strSQL_GPP, objConn<BR><BR> LocalArray(0) = CStr(containerID)<BR> LocalArray(1) = CStr(objRS_GPP("container_name"))<BR><BR> If Not IsNull(objRS_GPP("container_parentID")) Then<BR> Dim CalledArray()<BR> CalledArray = GetParentPath(objRS_GPP("container_parentID"))<BR> LocalArray(0) = LocalArray(0) & "," & CalledArray(0)<BR> LocalArray(1) = LocalArray(1) & "," & CalledArray(1)<BR> End If<BR><BR> GetParentPath = LocalArray<BR>End Function<BR><BR>I call this function with an argument corresponding to the ID in the table for which I&#039;d like to get the hierarchy. For example, if record with ID 20 has a parentID of 13 and record 13 has a parent of 5, I want to return a two-celled array. Cell 1 contains the string "13,20,5" and cell 2 contains a string with the names separated by commas. Anyway, my problem occurs at the line:<BR>CalledArray = GetParentPath(objRS_GPP("container_parentID"))<BR> in the If statement. The error is simply "Type mismatch" and I cannot figure out why. The function returns a 2-celled array. Any idea what&#039;s wrong here?<BR><BR>Thanks

  2. #2
    Join Date
    Dec 1969

    Default Just don't DIM it...

    Or, rather, just do<BR><BR>DIM CalledArray &#039; no parens!<BR><BR>But this will just get you to another type mismatch. You need to change this:<BR><BR>Dim LocalArray(2)<BR><BR>to this:<BR><BR>Dim LocalArray<BR>ReDim LocalArray(2)<BR><BR>Else when your GetParentPath function returns, the caller will get a type mismatch. And, of course, the caller needs to have done something like:<BR><BR>Dim resultArray &#039; no parens, no size<BR>resultArray = GetParentPath( xxxx )<BR><BR>See if all that doesn&#039;t make it work!<BR><BR>Incidentally, your array is dimensioned too big!<BR><BR>You should be doing<BR><BR>ReDim LocalArray(1) &#039; two elements<BR><BR>VBScript, unlike other languages, has you specify the UPPER BOUND of the array in the DIM, rather than the number of elements.<BR><BR>

Posting Permissions

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