How do I return multi-dimensional arrays?

Results 1 to 2 of 2

Thread: How do I return multi-dimensional arrays?

  1. #1
    Daniel Franks Guest

    Default How do I return multi-dimensional arrays?

    Hello,<BR><BR>I can&#039t figure out how to make this function return a usable two dimensional array to the code that called it. I keep getting the error "Type Mismatch". It works fine if I turn the below function into an one dimensional array and use "aryTemp = Array(intCreditReportID,strCompany,strLocation,str Mcnumber,strPhone)" instead. The error happens when I call the function and set its results to a variable. Then I pass that array to another function which displays the array. This is when I get the type mismatch.<BR><BR>Please help if you can,<BR>Thanks,<BR>Dan<BR><BR>p.s. This all ASP. No VB Components involved in this problem, though if neccessary I can use them.<BR><BR>=================BEGIN CODE=====================<BR>Function sqlGetCreditSearchResults(strSQL)<BR> Dim RS<BR> Dim Conn<BR><BR> Set RS = Server.CreateObject ("ADODB.Recordset")<BR> Set Conn = DataConnect<BR> RS.Open strSQL,Conn,1,3<BR><BR> If (Not RS.Eof) Then<BR> Dim intRecordCount <BR> Dim intCount <BR> Dim intCreditReportID <BR> Dim strCompany <BR> Dim strCity <BR> Dim strState <BR> Dim strLocation <BR> Dim strMcnumber <BR> Dim strPhone<BR><BR> intRecordCount = RS.RecordCount<BR> Dim aryTemp(1 to 5,1 to intRecordCount)<BR> <BR> Do While (Not RS.Eof)<BR> intCount = intCount + 1<BR> <BR> intCreditReportID = RS("lCREDRTPSID")<BR> strCompany = RS("COMPANY")<BR> strCity = RS("CITY")<BR> strState = RS("ST")<BR> If (strCity &#060;&#062; "") Then<BR> strLocation = strCity & ", " & strState<BR> Else<BR> strLocation = strState<BR> End If <BR> strMcnumber = RS("MCNUMBER")<BR> strPhone = RS("PHONE")<BR> <BR> aryTemp(1,intCount) = intCreditReportID<BR> aryTemp(2,intCount) = strCompany<BR> aryTemp(3,intCount) = strLocation<BR> aryTemp(4,intCount) = strMcnumber<BR> aryTemp(5,intCount) = strPhone<BR> <BR> RS.MoveNext<BR> Loop<BR> <BR> sqlGetCreditSearchResults = aryTemp<BR><BR> Set intRecordCount = Nothing<BR> Set intCount = Nothing<BR> Set intCreditReportID = Nothing<BR> Set strCompany = Nothing<BR> Set strCity = Nothing<BR> Set strState = Nothing<BR> Set strLocation = Nothing<BR> Set strMcnumber = Nothing<BR> Set strPhone = Nothing<BR> Set aryTemp = Nothing<BR> Else<BR> sqlGetCreditSearchResults = Null<BR> End If<BR> <BR> RS.Close<BR> Set RS = Nothing<BR> Set Conn = Nothing <BR>End Function<BR>=================END CODE=====================

  2. #2
    Join Date
    Dec 1969

    Default RE: How do I return multi-dimensional arrays?

    &nbsp;<BR>For starters, I didn&#039t think this was legal in VBScript:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;Dim aryTemp(1 to 5,1 to intRecordCount)<BR><BR>The MS docs even for version 5 of VBScript say you can&#039t specify the lowerbound, that it is always zero. Have you found that VBS 5 or 5.5 has added the lowerbound setting capability? Further, VBS supposedly doesn&#039t allow using a variable sized limit in a DIM statement. Again, has this changed in the version of VBS you are using? The MS online docs sure don&#039t reflect it!<BR><BR>In any case, the generically safe way of doing this kind of stuff in VBScript is usually to use ReDim.<BR><BR>So I would code that as...<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;Dim aryTemp( ) &#039 no sizes!<BR>&nbsp;&nbsp;&nbsp;&nbsp;ReDim aryTemp( 5, intRecordCount)<BR><BR>Don&#039t worry about the unused 0 elements...they don&#039t eat enough memory to be worth the bother if you are more comfortable starting at 1 (assuming that VBS 5.x or whatever you are using doesn&#039t allow the lower bound).<BR><BR>It can also be a bit tricky *receiving* the result of a function back into the caller. Again, you should use a Dim&#039ed to no particular size variable for this purpose. If you explicitly dim the variable, VBS will see a difference between the variable and the return value, even if they happen to be the same size. <BR><BR>So in your mainline code, you would do:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;Dim funcResults( )<BR>&nbsp;&nbsp;&nbsp;&nbsp;...<BR>&nbsp;&nbsp;&n bsp;&nbsp;funcResults = sqlGetCreditSearchResults("SELECT * FROM table")<BR><BR>If this doesn&#039t help, please let us know!<BR><BR><BR><BR><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