检查VBScript中是否存在对象 [英] Check if an Object exists in VBScript

查看:30
本文介绍了检查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: 替换

@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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆