1

我在构建数据集合时遇到问题。问题代码如下:

    'Basic defitions are as follows:
     Private mCol As Collection
     Dim mcnn As ADODB.Connection
     Dim mrs As New ADODB.Recordset

     Dim uCustomClass As CustomClass
     On Error GoTo 0
     'set Find to false to catch any errors
     Find = False
     'checks for an active connection and then..
     Set mCol = Nothing
     Set mCol = New Collection
     With mrs
     .Open AN_SQL_SELECT_STATEMENT , mcnn, adOpenForwardOnly, adLockOptimistic
     While Not .EOF
        Set uCustomClass = New CustomClass
        Set uCustomClass.Connection = mcnn
        uCustomClass.CustomerName = NullToEquiv(.Fields("customer_name").Value,NULL_STRING)
        uCustomClass.NumberOfOrders = NullToEquiv(.Fields("num_of_orders").Value, NULL_LONG)
        uCustomClass.FavoriteColour = NullToEquiv(.Fields("favorite_colour").Value, NULL_STRING)

        'Cache orginal values in case the keys change
        uCustomClass.CacheOriginalValues
        'add to collection
        mCol.Add uCustomClass
        .MoveNext
    Wend

现在运行时的结果是 uCustomClass 树结构如下所示:

    -uCustomClass
       +connection
        count
       + mcnn
       -mCol
          +Item1
          +Item2
          +Item3      
       +mrs
        mvarChangedCount
       +NewEnum

这一切都很好,我没有直接在 uCustomClass 下获得 Item1、Item2 和 Item3,而只是在 mCol 中。对于不同的自定义类,我似乎在其他地方运行完全相同的代码,我得到了我想要的,例如

    -uCustomClassThatWorks
        +connection
         count
        + mcnn
        -mCol
           +Item1
           +Item2
           +Item3      
        +mrs
        mvarChangedCount
        +NewEnum
        +Item1
        +Item2
        +Item3

问题可能出在哪里?

4

1 回答 1

1

不确定如何uCustomClass添加这些项目。是否有一些缺少的代码或什么?

值得一提的是,集合不能多次拥有相同的键,这可以解释为什么它们可以在一个区域中添加,但不能再次添加。可能有一些东西甚至会修剪字符串或会使情况恶化的东西。所以只要确保你的钥匙是独一无二的。

于 2013-08-21T13:39:51.230 回答