如何在VBA中实现自定义的迭代类 [英] How to implement custom iterable class in VBA

查看:622
本文介绍了如何在VBA中实现自定义的迭代类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的类添加一个功能,所以我可以在每个循环中使用它们。



我写了hashmaps,数组列表,队列,集合等我想迭代。
现在我正在寻找一种实现IUnknown类来构建自定义迭代器的方法。



我已经知道如何使用

 私有objPeople作为集合
公共属性获取NewEnum()作为IUnknown
属性NewEnum.VB_UserMemId = -4
属性NewEnum .VB_MemberFlags =40
设置NewEnum = objPeople。[_ NewEnum]
结束属性

但是所有这些示例都是基于Collection类,我不想使用。



我想要关注的是尝试实现IUnknown接口,但我还没有找到任何关于如何做的参考。



我在Java,C ++,C#等方面拥有丰富的经验,所以我认为即使在VBA中也可以使用API​​调用这样的方式。

解决方案

缺少的是你不能实现IUnknown 收集也是如此。它们都是无法在VBA中扩展的Com对象。您可以创建自定义集合并执行其他非常酷的迭代的东西


I want to add a feature to my classes so I can use them in for-each loops.

I wrote hashmaps, arraylists, queues, sets and so on that I want to iterate over. Now I'm looking for a way to implement the IUnknown class to build custom iterators.

I already know how to use

private objPeople as Collection
Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
    Set NewEnum = objPeople.[_NewEnum]
End Property

but all those examples out there are based on the Collection class, which I do not want to use.

What I want to focus on is trying to implement the IUnknown interface, but I haven't found any references on how to do that.

I have vast experience in Java, C++, C# and so on, so I assume that THERE HAS TO BE A WAY to implement that even in VBA, maybe even with API calls stuff like that.

解决方案

The long and short of it is you can't implement IUnknown. The same goes for Collection. They're both Com objects that are unavailable for extension in VBA. You can create custom collections and do other very cool iterable things though.

这篇关于如何在VBA中实现自定义的迭代类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆