Excel-VBA - VBA 中是否有类似 Javas Set 容器的东西? [英] Excel-VBA - Is there anything like Javas Set container in VBA?

查看:50
本文介绍了Excel-VBA - VBA 中是否有类似 Javas Set 容器的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 VBA 中有没有类似 Java 的 Set 容器的东西?我找不到任何东西,而且 Google 似乎没有帮助,因为 set 是 VBA 中的保留作品.

Is there anything like Java's Set container in VBA? I can't find anything and Google doesn't seem to helpful since set is a reserved work in VBA.

任何想法都会很棒.现在我唯一的选择是字典或数组.

Any ideas would be great. Right now my only options are Dictionary or Array.

谢谢.

推荐答案

VBA 有一个内置的Collection"对象,许多人认为 MS Scripting Runtime 中的Dictionary"对象足够标准,它本质上是其中的一部分VBA.

VBA has a 'Collection' object built in, and many people consider the 'Dictionary' object from the MS Scripting Runtime to be sufficiently standard that it's essentially part of VBA.

但是,两者都不会强制一般对象的唯一性.您是否可以让它们为您的应用程序强制执行唯一性取决于详细信息.

However, neither will enforce uniqueness for general objects. Whether or not you can make them enforce uniqueness for your application depends on the details.

例如,如果您想要一组字符串,这很容易.只需使用字典",并将其键用作您的集合".'Dictionary' 有一个 'Exists' 方法,因此编写自己的受限 'Set' 类非常容易,其中所有实际工作都由包含的 'Dictionary' 完成.(如果您使用 Excel,这将适用于任何简单的 Variant 值 - 字符串、数字、布尔值、错误.)

For example, if you want a set of strings, it's easy. Just use a 'Dictionary', and use its keys as your "set". 'Dictionary' has an 'Exists' method, so it would be pretty easy to write your own limited 'Set' class where all of the real work is done by a contained 'Dictionary'. (If you're using Excel, this will work with any simple Variant value - strings, numbers, booleans, errors.)

如果你有对象实例,事情会变得更复杂.字典"对象可以使用对象作为键,但它使用对象标识作为其相等性测试.例如,我只是在 VBA 即时窗口中输入了这个:

If you have object instances, things can get more complicated. The 'Dictionary' object can use objects as keys, but it uses object identity as its equality test. For example, I just typed this into the VBA Immediate window:

set d=new Dictionary
set c1=new Collection
set c2=c1
d(c1) = 42
fv d
<Dictionary: keys: #V(0..0){<Collection: 0 elems: >}, items: #V(0..0){42%}>
d(c2)=99
fv d
<Dictionary: keys: #V(0..0){<Collection: 0 elems: >}, items: #V(0..0){99%}>
set c3=new Collection
d(c3)=101
fv d
<Dictionary: keys: #V(0..1){<Collection: 0 elems: >,<Collection: 0 elems: >}, items: #V(0..1){99%,101%}>

('fv' 是我用于调试的 VBA 例程 - 它只是打印出内容的字符串表示形式.)您可以看到相同的对象被视为相等,但相同的 valued 对象是不.

('fv' is a VBA routine I use for debugging - it just prints out a string representation of stuff.) You can see that identical objects are treated as equal, but identically valued objects aren't.

如果您需要一个具有自定义相等性测试的集合,您需要编写重要的代码.但是,如果您至少可以编写从对象实例到可用作字典键的值的映射,您仍然可以避免编写自己的这个东西已经是集合的成员吗?"代码.

If you need a set that has a custom equality test, you'll need to write non-trivial code. However, if you can at least write a mapping from object instance to a value that can be used as a Dictionary key, you can still avoid having to write your own "is this thing already a member of the set?" code.

一些相关链接:

有没有办法为具有私有成员的 VBA 类编写相等测试而不暴露这些私有成员存在的知识?

VBA 中的哈希表/关联数组

这篇关于Excel-VBA - VBA 中是否有类似 Javas Set 容器的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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