vb 集合 是否可以以集合做成员变量如果可以,请问如何遍历,直到遍历到集合成员的最终类型。请给出代码。 求助!

解决方案 »

  1.   

    Private Sub Command1_Click()
        Dim ParentCollection As New Collection  '父集合
        Dim ChildCollection As New Collection     '子集合
        
        Dim i As Integer, j As Integer
        
        For i = 1 To 30
            ChildCollection.Add "项" & CStr(i)
        Next
        ParentCollection.Add ChildCollection   '父集合添加子集合
        
        Dim tempCollection As New Collection    '临时集合,用于遍历得到父集合中的子集合
        
        For i = 1 To ParentCollection.Count
            Set tempCollection = ParentCollection.Item(i)
            
            For j = 1 To tempCollection.Count
                Debug.Print tempCollection.Item(j)
            Next
        Next
    End Sub
      

  2.   

    真是太感谢了,你用VB多久了。还把代码做成 html格式的,一目了然。太感谢了!
      

  3.   

    应该先要判断是否集合 再考虑是否输出吧 递归比较容易实现
    Option Explicit
    Private Sub Command1_Click()
        Dim ParentCollection As New Collection  '父集合
        Dim ChildCollection As New Collection   '子集合
        Dim ChildCollection2 As New Collection  '孙集合
        Dim i As Integer, j As Integer
        For i = 1 To 30
          ChildCollection2.Add "孙子" & i
        Next
        For i = 1 To 15
          ChildCollection.Add "儿子" & i
        Next
        ParentCollection.Add ChildCollection  '父集合添加子集合
        ChildCollection.Add ChildCollection2  '子集合添加孙子集合
        Call OutputCol(ParentCollection)
    End Sub
    Private Sub OutputCol(ParentC As Collection, Optional CLevel As Long = 0)
      Dim i As Long
      If CLevel = 0 Then Debug.Print "根结点"
      For i = 1 To ParentC.Count
        If TypeOf ParentC.Item(i) Is Collection Then
          Debug.Print String(CLevel + 1, "-") & ">"
          Call OutputCol(ParentC.Item(i), CLevel + 1)
        Else
          Debug.Print String(CLevel, " ") & ParentC.Item(i)
        End If
      Next
    End Sub
      

  4.   

    我用的是vb6 ,怎么判断 add 的是obj , 还是 Collection 呢?
      

  5.   

    需求是什么?
    究竟是类似用来存储专一类型(比如File对象)的集合(FileCollection)实现除了Add、Item等属性以外还要实现 For...Each 功能。
    还是使用Collection时有疑问。请具体说明。
      

  6.   

    对不起我没说清楚,我原以为collection,可以自定义,却没想到自定义只能是class的集合,这不符合我的想法。
    我另发一贴,希望朋友们能帮我验证。
      

  7.   

    http://topic.csdn.net/u/20071217/17/8a8f0960-847b-43ca-b790-3e52d4b4b256.html