2014-10-10 68 views
1

我只是繼承了一箇舊項目,以保持使用asp和vbs。在vbs中,它傳回單個對象,並且我需要將其作爲一個數組,我嘗試過的所有內容都會給我一個關鍵的異常。我將SQL讀取從單個記錄轉換爲數組,但是我無法將其傳回到我需要的主要對象,而我的項目中沒有人知道vbs。VBScript不通過類返回數組

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 

任何幫助或建議,將不勝感激。

感謝, 喬希

回答

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) 
+0

非常感謝您!這個伎倆。 – 2014-10-10 17:24:08