I just discovered that I can create some .Net classes from VBA using the CreateObject method which can create COM classes. This is pretty cool but the created class is late
When you use
Dim enumerator As Object
you are declaring a COM object. But COM implements IEnumerator as IEnumVARIANT
, which inherits IUnknown
directly, and COM is not happy trying to set the GetEnumerator
return value into an Object
. Instead, you can use
Dim enumerator As IEnumVARIANT
EDIT: Note that although ArrayList
implements IEnumerable
, it overloads GetEnumerator with a method that takes index
and count
. So you have to use the Nothing
keyword in VBA when you want to call the overload with no parameters.
Set iterator = list.GetEnumerator(Nothing, Nothing)
You can see that it's working, because if you include your original lines of code that add two items to the list, and then use a call such as
Set iterator = list.GetEnumerator(1, 3) ' 3 is out of bounds
you now get a new (expected) error informing you that offset/length are out of bounds.