Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not sure if this is what you're looking for, but I'll give it a shot. If Car class contains one Speedometer class, Car contains Windspeed and Acceleration properties, Speedometer contains a Mass property, and speed is defined as Windspeed times Acceleration divided by Mass, then here's how I would set it up.</p> <p>In class CCar</p> <pre><code>Private mlCarID As Long Private mdWindSpeed As Double Private mdAcceleration As Double Private mclsSpeedometer As CSpeedometer 'Getters and setters Public Property Get CarID() As Long: CarID = mlCarID: End Property Public Property Let CarID(ByVal lCarID As Long): mlCarID = lCarID: End Property Public Property Get Acceleration() As Double: Acceleration = mdAcceleration: End Property Public Property Let Acceleration(ByVal dAcceleration As Double): mdAcceleration = dAcceleration: End Property Public Property Get WindSpeed() As Double: WindSpeed = mdWindSpeed: End Property Public Property Let WindSpeed(ByVal dWindSpeed As Double): mdWindSpeed = dWindSpeed: End Property 'read only property to the speedometer class Public Property Get Speedometer() As CSpeedometer Set Speedometer = mclsSpeedometer End Property 'create the child and set the parent property Private Sub Class_Initialize() Set mclsSpeedometer = New CSpeedometer Set mclsSpeedometer.Parent = Me End Sub Private Sub Class_Terminate() Set mclsSpeedometer.Parent = Nothing Set mclsSpeedometer = Nothing End Sub 'pass through property Public Property Get Speed() As Double Speed = Me.Speedometer.Speed End Property </code></pre> <p>In class CSpeedometer</p> <pre><code>Private mdMass As Double Private mclsParent As CCar Public Property Get Mass() As Double: Mass = mdMass: End Property Public Property Let Mass(ByVal dMass As Double): mdMass = dMass: End Property Public Property Get Parent() As CCar Set Parent = mclsParent End Property Public Property Set Parent(clsCar As CCar) Set mclsParent = clsCar End Property Public Property Get Speed() As Double 'references to parent properties Speed = Me.Parent.WindSpeed * Me.Parent.Acceleration / Me.Mass End Property </code></pre> <p>In a standard module</p> <pre><code>Sub GetSpeed() Dim clsCar As CCar Set clsCar = New CCar clsCar.CarID = 1 clsCar.WindSpeed = 10 clsCar.Acceleration = 5 clsCar.Speedometer.Mass = 100 Debug.Print clsCar.Speed End Sub </code></pre> <p>You have to make sure you destroy your parent/child relationship properly or you'll get a memory leak. I use CopyMemory to set up parent properties to avoid that particular problem. It's described here <a href="http://www.dailydoseofexcel.com/archives/2007/12/28/terminating-dependent-classes/#comment-29661" rel="nofollow">http://www.dailydoseofexcel.com/archives/2007/12/28/terminating-dependent-classes/#comment-29661</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload