views:

31

answers:

1

Hi, 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.

+1  A: 

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:

Option Explicit

Dim Hallway, DoorKnobs, DoorKnob

Set Hallway = CreateObject("HallLib.Hallway")
Set DoorKnobs = Hallway.DoorKnobs()

MsgBox "DoorKnobs.Count = " & CStr(DoorKnobs.Count)

For Each DoorKnob In DoorKnobs
    MsgBox "DoorKnob.Material = " & CStr(DoorKnob.Material) & vbNewLine _
         & "DoorKnob.Color = " & CStr(DoorKnob.Color)
Next

Update:

This script produces identical results:

Option Explicit

Dim Hallway, DoorKnobs, KnobIndex

Set Hallway = CreateObject("HallLib.Hallway")
Set DoorKnobs = Hallway.DoorKnobs()

MsgBox "DoorKnobs.Count = " & CStr(DoorKnobs.Count)

For KnobIndex = 1 To DoorKnobs.Count
    With DoorKnobs.Item(KnobIndex)
        MsgBox "DoorKnob.Material = " & CStr(.Material) & vbNewLine _
             & "DoorKnob.Color = " & CStr(.Color)
    End With
Next

As does:

Option Explicit

Dim Hallway, DoorKnobs, KnobIndex

Set Hallway = CreateObject("HallLib.Hallway")
Set DoorKnobs = Hallway.DoorKnobs()

MsgBox "DoorKnobs.Count = " & CStr(DoorKnobs.Count)

For KnobIndex = 1 To DoorKnobs.Count
    With DoorKnobs(KnobIndex)
        MsgBox "DoorKnob.Material = " & CStr(.Material) & vbNewLine _
             & "DoorKnob.Color = " & CStr(.Color)
    End With
Next

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.

Bob Riemersma