检查 VBScript 中是否存在对象 [英] Check if an Object exists in VBScript
问题描述
很久以前,我正在维护一个由外部公司用 VB 脚本编写的经典 ASP 应用程序.
I'm maintaining a Classic ASP app written in VB Script by an outside company long, long ago.
我有一组图像文件路径,如下所示:
I have an array of imagefile paths, like so:
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"
这仅存在于带有横幅广告的页面上.有一些标准代码在包含文件中遍历此列表(所有页面通用).
This will exist ONLY on pages that have banner ads. There is some standard code that iterates through this list in an include file (common to all pages).
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
错误
The problem is that if banners
is not instantiated on the page (it's not on all pages), I get a Can't find object
error
检查 VB 脚本中是否存在对象的正确方法是什么?
What's the proper way to check if an object exists in VB Script?
推荐答案
@Atømix: Replace
@Atømix: Replace
If Not banners Is Nothing then
并使用
If IsObject(banners) Then
然后您可以将其他代码放入包含文件并在页面顶部使用它以避免不必要的重复.
Your other code you can then place into an include file and use it at the top of your pages to avoid unnecessary duplication.
@Cheran S:我在打开/关闭 Option Explicit
的情况下测试了我上面的代码段,并且没有遇到任何版本的错误,无论 Dim Banners
是否存在或不是.:-)
@Cheran S: I tested my snippets above with Option Explicit
on/off and didn't encounter errors for either version, regardless of whether Dim banners
was there or not. :-)
这篇关于检查 VBScript 中是否存在对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!