asp-classic – 检查VBScript中是否存在Object

很久以前,我正在外部公司维护一个用VB Script编写的经典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

问题是如果横幅没有在页面上实例化(不在所有页面上),我得到一个无法找到对象错误

检查VB脚本中是否存在对象的正确方法是什么?

解决方法

@Atømix:替换
If Not banners Is nothing then

并使用

If IsObject(banners) Then

您的其他代码可以放在一个包含文件中,并在页面顶部使用它,以避免不必要的重复.

@Cheran S:我使用Option Explicit开启/关闭测试了我的片段,并且没有遇到任何版本的错误,无论Dim横幅是否存在.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....