Asp-Classic

檢查VBScript中是否存在對象

  • November 4, 2010

很久很久以前,我正在維護一個由外部公司用 VB 腳本編寫的經典 ASP 應用程序。

我有一組圖像文件路徑,如下所示:

dim banners, arrKeys, i
set banners=CreateObject("Scripting.Dictionary")
banners.Add "banner1.jpg", "http://www.somelink.com"
banners.Add "banner2.jpg", "http://www.somelink.com"
banners.Add "banner3.jpg", "http://www.somelink.com"

這將僅存在於具有橫幅廣告的頁面上。有一些標準程式碼在包含文件(所有頁面通用)中遍歷此列表。

If Not banners Is Nothing then 
 ' then loop through the Dictionary and make a list of image links
End if

問題是如果banners沒有在頁面上實例化(不是在所有頁面上),我得到一個Can't find object錯誤

檢查VB腳本中是否存在對象的正確方法是什麼?

@Atømix:替換

If Not banners Is Nothing then 

並使用

If IsObject(banners) Then 

然後,您可以將其他程式碼放入包含文件中,並在頁面頂部使用它以避免不必要的重複。

@Cheran S:我用Option Explicit開/關測試了上面的片段,無論是否Dim banners存在,都沒有遇到任何版本的錯誤。:-)

引用自:https://stackoverflow.com/questions/4100506