VBA连续类对象引用

我正在研究一个可用于计算通过pipe道系统的气体stream量特性的VBA项目。 pipe道系统由存储在由clsSegments类存储的集合中的多个段(VBA类; clsSegment)组成。

每个段都有一个Duct类(clsDuct),它由一个由clsDuctDim类定义的入口和出口组成。

如果我想要段号 2进风pipe道inheritance了段号的属性。 1出口pipe道我使用:

set Segments(2).Duct.Inlet = Segments(1).Duct.Outlet 

这工作。

同样,我可以select使用importpipe道作为出口pipe道:

 set segments(2).Duct.Outlet = segments(2).Duct.Inlet 

现在,如果我想要段号。 n + 2做同样的我写:

 set Segments(3).Duct.Inlet = Segments(2).Duct.Outlet set Segments(3).Duct.Outlet = Segments(3).Duct.Inlet 

这导致段号3实际指向段号。 ñ。 如果我打破了段2和段1之间的引用,段3仍然指向段1.这不是我想要的。 我希望段3指向段2.我想要做的是指向一个指针,而不是指向存储段1数据的实际内存位置。

这怎么能实现?

在VBA中没有直接的方法。 一个简单的解决方法是将一个复制或克隆方法添加到您的clsSegment类。

分段(3)。复制段(2)

 Sub Copy(segement As clsSegment) Me.Duct.Outlet = segement.Duct.Inlet Me.Duct.Inlet = segement.Duct.Outlet End Sub 

可以通过添加自定义事件并设置对父对象的引用来自动执行更改。 我不知道这将如何适用于您的系统,但这是一个简单的通知系统。

clsSegment

 Enum DuctEvents deInletRemoved deOutletRemoved End Enum Public Sub DuctChanged(e As DuctEvents) 'Do Something End Sub 

Class clsDuctDim

 Public Event DuctRemoved() Private Sub Class_Terminate() RaiseEvent DuctRemoved End Sub 

Class clsDuct

 Public parent As clsSegment Private WithEvents Inlet As clsDuctDim Private WithEvents Outlet As clsDuctDim Private Sub Inlet_DuctRemoved() parent.DuctChanged (deInletRemoved) End Sub Private Sub Outlet_DuctRemoved() parent.DuctChanged (deOutletRemoved) End Sub 

我向clsSegment类添加了一个Enumeration来清理消息。