Excel-VBA - 有什么像VBA中的Javas Set容器吗? [英] Excel-VBA - Is there anything like Javas Set container in VBA?

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

问题描述

有没有像VBA中的Java集合容器?我找不到任何东西,Google似乎没有帮助,因为设置是VBA中的保留工作。



任何想法都会很棒。现在我唯一的选择是Dictionary或Array。



谢谢。

解决方案

VBA内置了一个'Collection'对象,许多人认为Script对象从MS Scripting Runtime足够标准,它基本上是VBA的一部分。



但是,这两者都不会强制一般对象的唯一性。您是否可以使其强制执行您的应用程序的唯一性取决于详细信息。



例如,如果您想要一组字符串,这很容易。只需使用字典,并使用其键作为设置。 'Dictionary'有一个'Exists'方法,所以编写自己有限的'Set'类是很容易的,所有这些都是通过一个包含的'Dictionary'完成的。 (如果您使用的是Excel,则可以使用任何简单的Variant值 - 字符串,数字,布尔值,错误。)



如果有对象实例,事情可以得到更复杂。 'Dictionary'对象可以使用对象作为键,但它使用对象标识作为其等式测试。例如,我刚刚输入到VBA立即窗口中:

  set d = new Dictionary 
set c1 = new集合
set c2 = c1
d(c1)= 42
fv d
< Dictionary:keys:#V(0..0){< Collection:0 elems: >},项目:#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:>},项目:#V(0..1){99%,101%}>

('fv'是我用于调试的VBA例程 - 它只是打印出一个字符串表示形式)你可以看到,相同的对象被视为相等的,但是相同的价值对象不是。



如果你需要一个有一个自定义的等式测试,你需要编写非平凡的代码。但是,如果您至少可以将对象实例的映射写入可用作字典键的值,那么您仍然可以避免自己编写这个东西已经是集合的成员了吗?



一些相关链接:



有没有办法为VBA写平等测试私人会员的课程不会暴露这些私人会员的存在知识?



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.

Thanks.

解决方案

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.

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.)

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' 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.

Some relevane links:

Is there a way to write an equality test for a VBA class with private members without exposing knowledge of the existence of those private members?

Hash Table/Associative Array in VBA

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

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