2014-11-03 52 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,這(至少從誰不知道項目目的的角度)似乎並不做有用的東西,但無限的嵌套。