嵌套集合,访问元素types不匹配
我有两个藏品,第二个是第一个。 但是,当我尝试访问第二个集合的元素时,我得到一个Type mismatch
错误。
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)`应该给第二个集合的第一个元素。
代替
temp = coll1(1)(1)
使用
temp = coll1(2)(1)
使用coll1(2)(1)
会给dog
和coll1(1)
会给cat
。
为了使它更具可读性,你可以使用coll1.Item(2).Item(1)
作为coll1(2)(1)
,同样coll1.Item(1)
作为coll1(1)