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

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

问题描述

我想向我的类添加一个功能,以便我可以在 for-each 循环中使用它们.

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

我编写了哈希映射、数组列表、队列、集合等,我想对其进行迭代.现在我正在寻找一种方法来实现 IUnknown 类来构建自定义迭代器.

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.

我已经知道如何使用

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

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

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

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

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.

我在 Java、C++、C# 等方面拥有丰富的经验,所以我认为即使在 VBA 中也必须有一种方法来实现它,甚至可能使用类似的 API 调用.

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.

推荐答案

答案是可以做到,但是很丑(需要和IDL,两个.BAS 模块和两个 .cls 模块(其中之一是您的 Collection Class 模块).有关完整信息,请参阅此链接:

The answer is that it can be done, but it is ugly (requires and IDL, two .BAS modules and two .cls modules (one of which is your Collection Class module). For full information see this link:

在 VB 中创建您自己的超级收藏"

祝你好运!我需要做的事情似乎很复杂,所以现在我只是遍历 Collection 对象枚举器.

Good luck! It seemed to complicated for what I needed to do, so for now I just iterate over the Collection object enumerator.

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

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