Dim colEventText As New CollectionFor index = 1 To 4        
 Dim cnode As New clsNode
 cnode.strtext = index
 cnode.stateBegin = "test"                 colEventText.Add cnode
next 本意是循环每次生成新的cnode.但是结果都指定为同一个.
 collection里面添加的都是同一个对象. 请问, Dim cnode As New clsNode不能重新生成对象吗?

解决方案 »

  1.   

    呵呵你这样改,加一句:SET NOTHINGDim colEventText As New Collection
        
    For Index = 1 To 4
        Dim cnode As New clsNode
        cnode.strtext = Index
        cnode.stateBegin = "test"
          
        colEventText.Add cnode
        Set cnode = Nothing
    Next
      

  2.   

    不过这确实是个有意思的问题看上去最正规的写法应该是:    Dim colEventText As New Collection
        Dim cnode As clsNode
        
        For Index = 1 To 4
            Set cnode = New clsNode
            cnode.strtext = Index
            cnode.stateBegin = "test"
            
            colEventText.Add cnode
            Set cnode = Nothing
        Next但是如果写
        Dim colEventText As New Collection
        Dim cnode As new clsNode
        For Index = 1 To 4
            cnode.strtext = Index
            cnode.stateBegin = "test"
            
            colEventText.Add cnode
            Set cnode = Nothing
        Next
    也可以的,按理你如果把一个ADO的对想SET NOTHING了,你不可以再用里面的对想和方法,但是这个却可以
      

  3.   

    这个你要仔细理解New关键字了。可以这样:
    Dim colEventText As New CollectionFor index = 1 To 4        
     Dim cnode As clsNode
     set cnode=new clsnode
     cnode.strtext = index
     cnode.stateBegin = "test"                 colEventText.Add cnode
    next
      

  4.   

    楼主是问:
    Dim cnode As New clsNode不能重新生成对象吗?