Joshua Bengtson
Joshua Bengtson

Reputation: 103

VBScript not returning an array through a class

I just inherited an old project to maintain using asp and vbs. In vbs it passes back a single object and i need to make this into an array and everything I have tried ends up giving me a critical exception. I have my SQL read going from a single record into an array but I can't pass it back to my main object where I need it and nobody on my project knows vbs.

Class LoadFileBS
   Public Function getDocument(req, userProfile)
      Dim dao
      Set dao = new FileDAO   'define my class

      Dim document
      Set document = dao.retrieve(fileId) 'this was a single return that i'm trying to make into an array

If (checkAuthorization(document(1)) = true) Then 'tried making document an array and that didn't work for me.  The (1) was a test to just try and get the array working in this class and not meant as finalized code.

Class FileDAO
   Public Function Retrieve(fileId)
       'Run the query and return the results
       Set Retrieve = runDocumentQuery(fileId)      
   End Function

   Private Function runDocumentQuery(fileId)
      ... 'making SQL call and it returns and i fill document with the info.
      Dim document
      Set document = new DocumentTO
      Call document.setFileId(Trim(rs.fields(SQL_FILE_ID).value))
      ...
      Set documents(UBound(documents)) = document
      rs.MoveNext
      If (NOT rs.Eof) Then
        ReDim Preserve documents(UBound(documents) + 1)
      End If
      ...  'now try and return
      If (isObject(documents(1))) Then    
        Set runDocumentQuery = documents   '<-- array with multiple records
      Else
        Set runDocumentQuery = nothing
      End If
   End Function

Any help or tips would be appreciated.

Thanks, Josh

Upvotes: 1

Views: 133

Answers (1)

Bond
Bond

Reputation: 16311

Arrays aren't objects, so you don't use the Set statement. They can contains object references, however.

If (isObject(documents(1))) Then
    runDocumentQuery = documents    ' Return the array (removed "Set" here)
Else
    runDocumentQuery = Array()      ' Return empty array? Up to you.
End If

Then you'll need to do the same for your Retrieve function:

Public Function Retrieve(fileId)
    'Run the query and return the results
    Retrieve = runDocumentQuery(fileId)    ' (Removed "Set" here)
End Function

And, finally, back to your original call:

Dim document
document = dao.retrieve(fileId)    ' (Removed "Set" here)

Upvotes: 2

Related Questions