检查 VBScript 中是否存在对象
我很久以前就在维护一个由外部公司用 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 脚本中存在对象吗?
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
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
What's the proper way to check if an object exists in VB Script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
替换
并使用
您可以将其他代码放入包含文件中并在页面顶部使用它以避免不必要的重复。
我在
Option Explicit
打开/关闭的情况下测试了上面的代码片段,并且无论Dim Banners
是否存在,都没有遇到任何版本的错误。Replace
and use
Your other code you can then place into an include file and use it at the top of your pages to avoid unnecessary duplication.
I tested my snippets above with
Option Explicit
on/off and didn't encounter errors for either version, regardless of whetherDim banners
was there or not.IsObject
可以工作,但IsEmpty
可能是更好的选择 - 它专门用于检查变量是否存在或已初始化。总结一下:
IsEmpty(var)
将测试变量是否存在(没有对象显式),或者是否已初始化
IsNull(var)
将测试变量是否已分配给空
var Is Nothing
将测试变量是否已Set
到什么都没有
,但如果您在非对象IsObject(var)
将测试变量是否是对象(如果var< 显然仍会返回
False
/code> 是空
)。IsObject
could work, butIsEmpty
might be a better option - it is specifically intended to check if a variable exists or has been initialised.To summarize:
IsEmpty(var)
will test if a variable exists (without Object Explicit), or is initialisedIsNull(var)
will test if a variable has been assigned toNull
var Is Nothing
will test if a variable has beenSet
toNothing
, but will throw an error if you try it on something that isn't an objectIsObject(var)
will test if a variable is an object (and will apparently still returnFalse
ifvar
isEmpty
).如果声明了变量但未初始化,则其值将为
Empty
,您可以使用IsEmpty()
函数检查该值:banners
仅当您使用Set Banners = Nothing
显式为其指定该值时,才会等于Nothing
。但是,如果您打开了
Option Explicit
(这是建议,但情况并非总是如此),则使用此技术会遇到问题。在这种情况下,如果banners
尚未Dim
,并且您尝试测试IsEmpty(banners)
,您将收到运行时错误。如果您没有启用Option Explicit
,则应该不会有任何问题。编辑:我刚刚看到这个 相关的问题和答案也可能有帮助。
If a variable is declared, but not initialized, its value will be
Empty
, which you can check for with theIsEmpty()
function:banners
will only be equal toNothing
if you explicitly assign it that value withSet banners = Nothing
.You will have problems, though, with this technique if you have
Option Explicit
turned on (which is the recommendation, but isn't always the case). In that case, ifbanners
hasn't beenDim
ed and you try to testIsEmpty(banners)
, you will get a runtime error. If you don't haveOption Explicit
on, you shouldn't have any problems.edit: I just saw this related question and answer which might help, too.
有点相关的是
IsMissing()
来测试是否传递了可选参数,在本例中是一个对象,如下所示:Somewhat related is
IsMissing()
to test if an optional parameter was passed, in this case an object, like this:IsEmpty、Is Object、IsNull 都不能与“Option Explicit”设置一起使用,正如上面的 Stealthyninja 所误导的答案。
我知道的唯一方法是使用“On Error Resume Next”设置来“破解”“Option Explicit”,正如 Tristan Havelick 在这里所做的那样:
有什么办法吗检查是否定义了 VBScript 函数?
Neither of IsEmpty, Is Object, IsNull work with the "Option Explicit" Setting, as stealthyninja above has misleadingly answered.
The single way i know is to 'hack' the 'Option Explicit' with the 'On Error Resume Next' setting, as Tristan Havelick nicely does it here:
Is there any way to check to see if a VBScript function is defined?
每个页面上至少需要有
暗淡横幅
。每个页面上都没有
head.asp
或其他内容吗?You need to have at least
dim banners
on every page.Don't you have a
head.asp
or something included on every page?