本文介绍了嵌套集合,访问元素类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个集合,第二个集合在第一个集合中.但是,当我尝试访问第二个集合的元素时,出现了Type mismatch错误.

I have two collections, the second one is inside the first one. But when I try to access the element of the second collection I get a Type mismatch error.

Sub testColls()
Dim coll1 As Collection
Dim coll2 As Collection

Set coll1 = New Collection
Set coll2 = New Collection

coll2.Add ("dog")
coll1.Add ("cat")

coll1.Add coll2

Dim temp As String
temp = coll1(1)(1)
MsgBox (temp)

End Sub

为什么会出现此错误? coll1(1)获取第二个集合,coll(1)(1)`应该给出第二个集合的第一个元素.

Why is this error? coll1(1) gets the second collection, coll(1)(1)` should give first element of the second collection.

推荐答案

代替

temp = coll1(1)(1)

使用

temp = coll1(2)(1)

使用coll1(2)(1)将给出dog,而coll1(1)将给出cat.

Using coll1(2)(1) will give dog and coll1(1) will give cat.

要使其更具可读性,您可以对coll1(2)(1)使用coll1.Item(2).Item(1),对于coll1(1)

To make it more readable you can use coll1.Item(2).Item(1) for coll1(2)(1) and likewise coll1.Item(1) for coll1(1)

这篇关于嵌套集合,访问元素类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-07 20:37