Wrap .Net ArrayList with custom VBA class get iterator

后端 未结 1 1779
小蘑菇
小蘑菇 2020-12-30 09:28

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

相关标签:
1条回答
  • 2020-12-30 09:56

    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.

    0 讨论(0)
提交回复
热议问题