【发布时间】:2017-08-07 14:00:04
【问题描述】:
我正在尝试在旧集合之上使用新密钥构建集合。我看过其他类似的主题,但它们都有我无法与我相关的特定问题。我的集合问题要求我从旧集合 (Col_0) 中获取一个对象,将其拆分为属性值略有不同的孪生对象,然后将两者放入一个新集合中,并使用基于一个的键的改变的属性。我的问题是更改后的双胞胎会覆盖新集合中的原始双胞胎。下面是一个简单的例子:
Public Sub FillOutput(ID As String, Col_0 As Collection)
'---Obj is a class scope object not defined here.
'---ID is the item key for items in Col_0 collection
Dim Col_1 As Collection
Set Obj_0 = Col_0.Item(ID)
'---Now I have the right item from the collection
Loop_Count = 0
Set Col_1 = New Collection
While Loop_Count < 2
If Loop_Count = 0 Then
Obj_0.Color = "Red"
Col_1.Add Obj_0, CStr(Obj_0.Color)
ElseIf Loop_Count = 1 Then
Obj_0.Color = "Blue"
Col_1.Add Obj_0, CStr(Obj_0.Color)
End If
Loop_Count = Loop_Count + 1
Wend 'Loop_count
End If
End Sub
我尝试了很多方法,例如创建一个临时对象,该对象在循环内被初始化并设置为空。如下:
ElseIf Loop_Count = 1 Then
Set pObj = New clsObjTable
pObj = Obj_0
pObj.Color = "Blue"
Col_1.Add pObj, CStr(pObj.Color)
Set pObj = Nothing
End If
还是不行。我的最终收藏不断被覆盖,因此这两个项目都是“蓝色”。我怀疑它是关于密钥被转移和搞砸的,但我在这里还不够了解。任何帮助或指导将不胜感激!
【问题讨论】:
标签: ms-access collections vba