2014-11-03 82 views
-1

这是我的问题的屏幕,这是无限量的收藏。 我希望将集合添加到对象属性只需一次。不喜欢这样的:无限量收藏VBA

http://postimg.org/image/o6da95j0f/ (屏幕显示在VBA

“看”集合
Public Sub testCollections() 

Dim index As Long 
index = 1 
    Dim OJsonElement As JsonElement 
    Dim newColl As New Collection 
    Dim str As String 
    Call addColl(OJsonElement, newColl) 


    For Each OJsonElement In newColl 
    Debug.Print "THE NAME IS:" & OJsonElement.name 
    Next OJsonElement 

End Sub 


Function addColl(obj1 As JsonElement, nextCollection As Collection) 
Dim i As Long 
Set nextCollection = New Collection 
Set obj1 = New JsonElement 
Set obj1.valueCollection = nextCollection 
obj1.name = "CityName" 
obj1.value = "type" 
nextCollection.Add obj1 


'obj1.ValueType = nextCollection 


'nextCollection.Add nextCollection 



End Function 

Class: 
Public name As String 
Public nameCollection As Collection 
Public value As Variant 
Public ValueType As String 
Public valueCollection As Collection 

回答

0

我真的不明白你的好代码,但我会限制解释你为什么会发生这个问题。你在你的观察者看到的行:

Set obj1.valueCollection = nextCollection 

是增加新的集合为obj1财产valueCollection然后,两行之后,你说:

nextCollection.Add obj1 

这意味着您要将obj1添加到它自己的属性中,因此创建了无限嵌套。我想帮助你,但为此,我需要了解你想用你的代码达成什么。但坚持您的请求我想收集只需添加到对象属性,我只是建议您删除行nextCollection.Add obj1,这(至少从谁不知道项目目的的角度)似乎并不做有用的东西,但无限的嵌套。