Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_web_howto thread: asp - Pass array to a function


Message #1 by "April Lafferty" <agl@c...> on Fri, 12 Apr 2002 00:49:57
I am trying to pass an array to a function that will sort it. I can 
populate the array no problem and print the values contained in the array 
but I am unable to pass it successfully.  Any help would be appreciated.  
My code is below. I have tried several different ways but have no luck.

thanks in advance

'count all values of parameter that are not zero
Do while not rstsimple.eof
If Len(rstsimple.fields("temp")) > 0 then
        pctr =pctr+1
End if
Rstsimple.movenext
loop
'dim the array to the number of values found
ReDim Preserve parmArr(pctr)
'load the array
pctr=0
Set rstSimple=GetRecordset(strDBPath, strsql)
Do while not rstsimple.eof
If Len(rstsimple.fields("temp")) > 0 then
        pctr =pctr+1
        parmArr(pctr)=rstsimple.fields("temp")

End if
Rstsimple.movenext
loop
Set finalArr() = sortarray(parmArr(), sctr)

Function sortarray(passarr(), sctr)
redim preserve passarray(sctr)

'bubble sort function
'I will fine tune this to a better sorting 
'function after I get the passing to work
For j = LBound(passarr) to (UBound(passarr)-1)
        For i = LBound(passarr) to (UBound(passarr)-1)
         if passArr(i) > passArr(i+1) Then
                temp = passArr(i)
                passArr(i) =  passArr(i+1)
                passArr(i+1) = temp
         End if
        Next 
Next

'pass the value back
Set sortarray = passarr
End Function
Message #2 by "Ken Schaefer" <ken@a...> on Fri, 12 Apr 2002 17:00:58 +1000
Here's a hint:

a) When constructing the recordset, only return records where the field
called Temp isn't NULL, or doesn't contain a zero length string. That will
obviate the need for the first loop

strSQL _
    "SELECT Temp " & _
    "FROM table " & _
    "WHERE Temp IS NOT NULL"

b) Once you have the recordset, use the Recordset's inbuilt GetRows method
to populate the array:

<%
objRS.Open strSQL, objConn
If not objRS.EOF then
    arrResults = objRS.getRows
End If
%>

viola, you have an array.

Now, to pass the array:

<%
If isArray(arrResults) then
    DoStuff(arrResults)
End If
'
'
Function DoStuff( _
    ByVal arrResults _
    )

End Function
%>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "April Lafferty" <agl@c...>
Subject: [asp_web_howto] asp - Pass array to a function


: I am trying to pass an array to a function that will sort it. I can
: populate the array no problem and print the values contained in the array
: but I am unable to pass it successfully.  Any help would be appreciated.
: My code is below. I have tried several different ways but have no luck.
:
: thanks in advance
:
: 'count all values of parameter that are not zero
: Do while not rstsimple.eof
: If Len(rstsimple.fields("temp")) > 0 then
:         pctr =pctr+1
: End if
: Rstsimple.movenext
: loop
: 'dim the array to the number of values found
: ReDim Preserve parmArr(pctr)
: 'load the array
: pctr=0
: Set rstSimple=GetRecordset(strDBPath, strsql)
: Do while not rstsimple.eof
: If Len(rstsimple.fields("temp")) > 0 then
:         pctr =pctr+1
:         parmArr(pctr)=rstsimple.fields("temp")
:
: End if
: Rstsimple.movenext
: loop
: Set finalArr() = sortarray(parmArr(), sctr)
:
: Function sortarray(passarr(), sctr)
: redim preserve passarray(sctr)
:
: 'bubble sort function
: 'I will fine tune this to a better sorting
: 'function after I get the passing to work
: For j = LBound(passarr) to (UBound(passarr)-1)
:         For i = LBound(passarr) to (UBound(passarr)-1)
:          if passArr(i) > passArr(i+1) Then
:                 temp = passArr(i)
:                 passArr(i) =  passArr(i+1)
:                 passArr(i+1) = temp
:          End if
:         Next
: Next
:
: 'pass the value back
: Set sortarray = passarr
: End Function
:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


  Return to Index