从 HTML 中嵌入的 VBScript 访问 VB6 集合项
我正在通过实践来学习。我得到了一个 OCX 文件,根据谁给我的说法,该文件是使用 VB6 创建的,我的任务是为其创建一个用户界面,以测试一个写得不好的文档文件中描述的所有功能。最重要的是,我不太精通 VBScript,但在学习过程中我成功地躲过了一些子弹。
我有一个返回 Collection
的方法,当我尝试从 VBScript 访问它时,我只能查询 Count
但当我尝试执行 job 时。 Item(i)
或 job(i)
我收到一条错误,指出它没有该属性或方法。
有人可以为我指明正确的方向,以便能够遍历该集合的内容吗?
我必须使用 JavaScript 来完成此任务,但由于有些事情并不那么容易,我决定也许 VBScript 可以帮助我弥补 JavaScript 无法解决的空白。我可以通过 JavaScript 访问 ActiveXObject 的所有属性,但返回其他 VB 对象的方法对我来说有点晦涩难懂。我尝试过 aJob.Item(iCount)
、aJob.Items(iCount)
和 aJob(iCount)
。
我的代码是:
For iCount = 1 To aJobs.Count
MsgBox("Num " & iCount)
MsgBox(aJobs.Item(iCount))
Next
谢谢。
i'm learning by practice. I was given an OCX file which according to who gave it to me was created using VB6 and I have the task of creating a user interface for it to test all the functionality that is described in a poorly written documentation file. On top of that I am not well-versed in VBScript but I've managed to dodge a few bullets while learning.
I have a method which returns a Collection
and when I try to access it from VBScript I am only able to query the Count
but when I try to do job.Item(i)
or job(i)
I get an error stating it doesn't have that property or method.
Can someone point me in the right direction to be able to traverse the contents of this collection?
I had to do it from JavaScript but since some things weren't that easy I decided that perhaps VBScript would help me bridge the gaps where JavaScript didn't cut it. I can access all properties from the ActiveXObject from JavaScript, but the methods which return other VB objects are a little more obscure to me. I've tried aJob.Item(iCount)
, aJob.Items(iCount)
and aJob(iCount)
.
My code is:
For iCount = 1 To aJobs.Count
MsgBox("Num " & iCount)
MsgBox(aJobs.Item(iCount))
Next
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
人们经常在 VB6 中创建专门的和/或强类型的集合类。但他们并不总是正确地做到这一点,有时他们会创建没有 Item() 方法的“部分”集合实现(或者无法将其标记为类的默认成员)。他们甚至可能有类似的方法或属性,但将其命名为完全不同的名称。
返回原始 Collection 对象的情况很少见,但这是可以完成的,如果是的话,您就不应该遇到 VBScript 中指出的问题。
我刚刚创建了一个名为“HallLib”的 DLL 项目,其中包含三个类:Hallway、DoorKnobs 和 DoorKnob。 DoorKnobs 类是 DoorKnob 对象的集合。 Hallway 类有一个 DoorKnobs 对象,它使用一组具有随机设置属性的随机 DoorKnob 对象进行初始化。 Hallway.DoorKnobs() 返回 DoorKnobs 集合对象作为其结果。
它在这个脚本中工作正常:
更新:
这个脚本产生相同的结果:
一样:
所以我怀疑你需要使用一些类型库浏览器,如 OLEView 来查看你的 OCX 以查看哪些类和它实际暴露的成员。
People often create specialized and/or strongly typed collection classes in VB6. They don't always do it correctly though, and they sometimes create "partial" collection implementations that have no Item() method (or fail to mark it as the default member of the class). They might even have a similar method or property but name it something entirely different.
It is rarer to return a raw Collection object, but it can be done and if it is you shouldn't have the problems you have indicated from VBScript.
I just created a DLL project named "HallLib" with three classes: Hallway, DoorKnobs, and DoorKnob. The DoorKnobs class is a collection of DoorKnob objects. The Hallway class has a DoorKnobs object that it initializes with a random set of DoorKnob objects with randomly set properties. Hallway.DoorKnobs() returns the DoorKnobs collection object as its result.
It works fine in this script:
Update:
This script produces identical results:
As does:
So I suspect you'll need to use some type library browser like OLEView to look at your OCX to see what classes and members it actually exposes.