检查 VBScript 中是否存在对象

发布于 2024-09-30 20:14:42 字数 647 浏览 2 评论 0原文

我很久以前就在维护一个由外部公司用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

心在旅行 2024-10-07 20:14:42

替换

If Not banners Is Nothing then 

并使用

If IsObject(banners) Then 

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

我在 Option Explicit 打开/关闭的情况下测试了上面的代码片段,并且无论 Dim Banners 是否存在,都没有遇到任何版本的错误。

Replace

If Not banners Is Nothing then 

and use

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.

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.

泛滥成性 2024-10-07 20:14:42

IsObject 可以工作,但 IsEmpty 可能是更好的选择 - 它专门用于检查变量是否存在或已初始化。

总结一下:

IsObject could work, but IsEmpty 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 initialised
  • IsNull(var) will test if a variable has been assigned to Null
  • var Is Nothing will test if a variable has been Set to Nothing, but will throw an error if you try it on something that isn't an object
  • IsObject(var) will test if a variable is an object (and will apparently still return False if var is Empty).
兔小萌 2024-10-07 20:14:42

如果声明了变量但未初始化,则其值将为 Empty,您可以使用 IsEmpty() 函数检查该值:

Dim banners
If IsEmpty(banners) Then
    Response.Write "Yes"
Else
    Response.Write "No"
End If
' Should result in "Yes" being written

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 the IsEmpty() function:

Dim banners
If IsEmpty(banners) Then
    Response.Write "Yes"
Else
    Response.Write "No"
End If
' Should result in "Yes" being written

banners will only be equal to Nothing if you explicitly assign it that value with Set 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, if banners hasn't been Dimed and you try to test IsEmpty(banners), you will get a runtime error. If you don't have Option Explicit on, you shouldn't have any problems.

edit: I just saw this related question and answer which might help, too.

如若梦似彩虹 2024-10-07 20:14:42

有点相关的是 IsMissing() 来测试是否传递了可选参数,在本例中是一个对象,如下所示:

Sub FooBar(Optional oDoc As Object)

    'if parameter is missing then simulate it
    If IsMissing(oDoc) Then Dim oDoc as Object: oDoc = something

...

Somewhat related is IsMissing() to test if an optional parameter was passed, in this case an object, like this:

Sub FooBar(Optional oDoc As Object)

    'if parameter is missing then simulate it
    If IsMissing(oDoc) Then Dim oDoc as Object: oDoc = something

...
飘落散花 2024-10-07 20:14:42

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?

小镇女孩 2024-10-07 20:14:42

每个页面上至少需要有暗淡横幅

每个页面上都没有 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文