简体   繁体   English

VBA连续类对象参考

[英]VBA Successive Class object reference

I am working on a VBA project that can be used to calculate properties of gas flow through a duct system. 我正在研究一个VBA项目,该项目可用于计算通过管道系统的气流特性。 The duct system consists of a number of Segments (a VBA class; clsSegment) stored in a Collection stored by the class clsSegments. 风管系统由存储在由clsSegments类存储的Collection中的多个Segment(VBA类; clsSegment)组成。

Each Segment has a Duct class (clsDuct) which consists of an Inlet and Outlet defined by the class clsDuctDim. 每个段都有一个Duct类(clsDuct),该类由clsDuctDim类定义的Inlet和Outlet组成。

If I want Segment no. 如果我要段号。 2 Inlet Duct to inherit the properties of Segment no. 2进气管继承了段号的属性。 1 Outlet Duct I use: 我使用的1个风管:

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

This works. 这可行。

Likewise I can choose to use the inlet duct as outlet duct by: 同样,我可以通过以下方式选择将入口导管用作出口导管:

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

Now if I want Segment no. 现在,如果我要段号。 n+2 to do the same I write: n + 2做同样的事情,我写:

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

This causes Segment no 3 to actually point to Segment no. 这会导致3号段实际上指向3号段。 n. ñ。 If I break the reference between Segments 2 and 1, Segment 3 still points to Segment 1. This is not what I want. 如果我在段2和1之间断开引用,则段3仍指向段1。这不是我想要的。 I want that Segment 3 is pointing to Segment 2. I guess what I am trying to do is to point to a pointer, rather than point to the actual memory location where segment 1 data is stored. 我希望段3指向段2。我想我要尝试的是指向一个指针,而不是指向存储段1数据的实际内存位置。

How can this be achieved? 如何做到这一点?

There is no direct way to do that in the VBA. 在VBA中没有直接的方法可以做到这一点。 An easy workaround is to add a Copy or Clone method to your clsSegment Class. 一个简单的解决方法是向您的clsSegment类添加一个Copy或Clone方法。

Segments(3).Copy Segments(2) 细分(3)。复制细分(2)

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

The can automate changes by adding custom events and setting references to parent object. 可以通过添加自定义事件并设置对父对象的引用来自动执行更改。 I'm not sure how this would apply to your system but here is a simple notification system. 我不确定这将如何应用于您的系统,但这是一个简单的通知系统。

clsSegment clsSegment

Enum DuctEvents
    deInletRemoved
    deOutletRemoved
End Enum

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

Class clsDuctDim 类clsDuctDim

Public Event DuctRemoved()

Private Sub Class_Terminate()
    RaiseEvent DuctRemoved
End Sub

Class clsDuct 类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

I added an Enumeration to the clsSegment class to clean up the messaging. 我在clsSegment类中添加了一个枚举,以清理消息传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM