用数组填充集合 [英] Populating collection with arrays

查看:160
本文介绍了用数组填充集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Dim A As Collection 
Set A = New Collection

Dim Arr2(15,5)
Arr2(1,1) = 0
'...

A.Add(Arr2)

如何通过 A 访问 Arr2 ?例如,我想执行以下操作:

  A.Item(1)(1,1)= 15 

所以上面将改变集合中第一个二维数组的第一个元素...

解决方案

嗯...语法看起来合法,没有VBA在我面前。我是对的,你的问题是你的代码编译并执行而不会引发错误,但是集合中的数组永远不会改变?如果是这样,我认为这是因为您的A.Item(1)可能会返回您存储在集合中的数组的复制。然后,您可以访问和修改所选元素,但它没有效果,因为它是错误的数组实例。



一般来说,VBA集合在存储对象时效果最好。然后他们会像你想要的那样工作,因为他们存储引用。它们对于存储值是很好的,但我认为它们总是复制它们,甚至是大的像数组变体,这意味着你不能突变存储数组的内容。



考虑这个答案只是一个猜测,直到知道底层COM东西的人更重要。呃...寻呼Joel Spolsky?



编辑:在Excel VBA中尝试过之后,我想我是对的。将一个数组变量放在一个集合中,可以复制一个拷贝,同样也可以得到一个。所以似乎没有直接的方法来代码你实际要求的内容。



看起来你真正想要的是一个3-D数组,但是你在使用第一个维度的集合的事实意味着你想成为能够修改该维度中的大小。 VBA只允许您更改数组的最后维度的大小(请参阅帮助中的redim preserve)。您可以将您的2-D数组放在1-D数组中,您可以更改大小:

  ReDim a(5)
Dim b(2,2)
a(2)= b
a(2)(1,1)= 42
ReDim Preserve a(6)

请注意,在这种情况下,使用ReDim声明,而不是Dim。 >

最后,您尝试做的任何其他方法都有可能会更好。至少可以说,可扩展的,可变的3-D阵列是复杂且容易出错的。


Dim A As Collection
Set A = New Collection

Dim Arr2(15, 5)
Arr2(1,1) = 0
' ...

A.Add (Arr2)

How can I access the Arr2 through A? For example, I want to do the following:

A.Item(1) (1,1) = 15

so the above would change the first element of the first two-dimensional array inside the collection...

解决方案

Hmmm...the syntax looks legal enough without having VBA in front of me. Am I right that your problem is that your code "compiles" and executes without raising an error, but that the array in the collection never changes? If so, I think that's because your A.Item(1) might be returning a copy of the array you stored in the collection. You then access and modify the chosen element just fine, but it's not having the effect you want because it's the wrong array instance.

In general VBA Collections work best when storing objects. Then they'll work like you want because they store references. They're fine for storing values, but I think they always copy them, even "big" ones like array variants, which means you can't mutate the contents of a stored array.

Consider this answer just a speculation until someone who knows the underlying COM stuff better weighs in. Um...paging Joel Spolsky?

EDIT: After trying this out in Excel VBA, I think I'm right. Putting an array variant in a collection makes a copy, and so does getting one out. So there doesn't appear to be a direct way to code up what you have actually asked for.

It looks like what you actually want is a 3-D array, but the fact that you were tyring to use a collection for the first dimension implies that you want to be able to change it's size in that dimension. VBA will only let you change the size of the last dimension of an array (see "redim preserve" in the help). You can put your 2-D arrays inside a 1-D array that you can change the size of, though:

ReDim a(5)
Dim b(2, 2)
a(2) = b
a(2)(1, 1) = 42
ReDim Preserve a(6)

Note that a is declared with ReDim, not Dim in this case.

Finally, it's quite possible that some other approach to whatever it is you're trying to do would be better. Growable, mutable 3-D arrays are complex and error-prone, to say the least.

这篇关于用数组填充集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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