How do I make my custom class compatible with For Every?

Can I create my own VBScript-only container class (no COM objects) with a For Each statement? If so, what methods should I disclose?

+3


source to share


1 answer


In short, no

Why? To create an enumerable collection class to get something like

Class CTest
    ....
End Class

Dim oTest, mElement
    Set oTest = New CTest
    ....
    For Each mElement In oTest
        ....
    Next 

      

the class MUST follow certain rules. We need a class to express

  • A public readonly property called Count

  • Public default method Item

  • A public readonly property called _NewEnum

    which should return IUnknown

    to the object that implements the interface IEnumVARIANT

    and which should have a hidden attribute and send id -4



And from this list or requirement, VBScript does not include any way to specify a submit ID or hidden property attribute.

So it can't be done

The only way to list the items stored in a container class is to have a property (or method) that returns

  • an object that supports all of the specified requirements, usually the same object that is used to store the items (fast, but it will display too much information)

  • an array (can be enumerated in VBScript arrays) containing references to each of the elements in the container (slow if the array needs to be generated when called, but does not return any unnecessary information)

+7


source







All Articles