Nested collections, access elements type mismatch

左心房为你撑大大i 提交于 2020-02-29 06:10:48

问题


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

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


回答1:


Instead of

temp = coll1(1)(1)

use

temp = coll1(2)(1)

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

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)



来源:https://stackoverflow.com/questions/46191127/nested-collections-access-elements-type-mismatch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!