如何在存储在集合中时更改Class属性的值

我想在一个集合中存储一个类,并且能够改变这个类的属性,而不必删除这个集合项并重新添加它。

我的研究表明,如果没有移除/replace操作,物品本身不能被改变,但是物品的属性如何呢?

下面的代码显示了如何做到这一点。 当您运行macros时,debugging窗口将显示存储对象的初始值和已更改的值。

如果您不使用Key ,则需要通过其索引编号引用收集项目。

类模块

 Option Explicit 'RENAME cNodes Private pNode1 As Variant Private pNode2 As Variant Public Property Get Node1() As Variant Node1 = pNode1 End Property Public Property Let Node1(Value As Variant) pNode1 = Value End Property Public Property Get Node2() As Variant Node2 = pNode2 End Property Public Property Let Node2(Value As Variant) pNode2 = Value End Property 

常规模块

 Option Explicit Sub ChangeCollectionItem() Dim COL As Collection, cN As cNodes Dim sKey As String Dim a, b Set COL = New Collection a = 1 b = 2 Set cN = New cNodes With cN .Node1 = a .Node2 = b sKey = a & "|" & b COL.Add Key:=sKey, Item:=cN Debug.Print COL(sKey).Node1, COL(sKey).Node2 '--> 1 2 With COL(sKey) .Node1 = .Node1 * 10 .Node2 = .Node2 * 5 End With Debug.Print COL(sKey).Node1, COL(sKey).Node2 '--> 10 10 End With End Sub