VBScript没有通过类返回数组

时间:2014-10-10 14:50:56

标签: arrays class vbscript

我刚刚继承了一个旧项目来维护使用asp和vbs。在vbs中它传回一个对象,我需要将它变成一个数组,我所尝试的一切最终都给了我一个关键的例外。我将我的SQL读取从单个记录转换为数组,但我无法将其传递回我需要它的主对象,并且我的项目中没有人知道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

任何帮助或提示都将不胜感激。

谢谢, 约什

1 个答案:

答案 0 :(得分:2)

数组不是对象,因此不要使用Set语句。但是,它们可以包含对象引用。

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

然后,您需要对Retrieve函数执行相同的操作:

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

最后,回到原来的电话:

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