I'm working through a problem I have with Excel VBA. I have two object classes. Class clsLoadStep stores three copies of class clsLoad objects. Class clsLoad simply stores a value and a string stating where that value came from. One clsLoad stores a max value and another a min value. My problem comes to read/write access to the nested clsLoad objects in the clsLoadStep. Currently the properties of the clsLoad objects of clsLoadStep can be changed (via the Get property then the Let property of the clsLoad object) which can result in the Max object storing a value less than the Min object. Is there a way to restrict access to the clsLoad objects in the clsLoadStep object to read-only while still letting the definition of the clsLoad class have both read and write properties? I want to be able to read the properties of the clsLoad objects but not change them.
I have a function in the clsLoadStep object that checks if min > max and swaps the object references if that's the case. The clsLoadStep object still needs to be able to tell if the clsLoad object's values have changed. Is there a recommended way to accomplish this?
For a while I had a "Parent" property of the clsLoad object that pointed to the clsLoadStep object. This allowed the clsLoadStep object to know when the clsLoad object changed (by calling the UpdateMaxMin function of the parent). That was a problem though as mulitple objects could reference the clsLoad object and it would be restricted to one parent.
Class clsLoad
Option Explicit
Private pValue As Double
Private pSource As String
'*****************************************************************
'P R O P E R T I E S
'*****************************************************************
'''''''''''''''''''''''''
' Value property
'''''''''''''''''''''''''
Public Property Get Value() As Double
Value = pValue
End Property
Public Property Let Value(ByVal Val As Double)
pValue = Val
End Property
'''''''''''''''''''''''''
' Source property
'''''''''''''''''''''''''
Public Property Get Source() As String
Source = pSource
End Property
Public Property Let Source(ByVal Val As String)
pSource = Val
End Property
Class clsLoadStep
Option Explicit
Private pMax As clsLoad
Private pMin As clsLoad
Private pOcc As clsLoad
Private pSource As String
'*****************************************************************
'P R O P E R T I E S
'*****************************************************************
'''''''''''''''''''''''''
' Max property
'''''''''''''''''''''''''
Public Property Get Max() As clsLoad
Set Max = pMax
UpdateMaxMin
End Property
Public Property Set Max(ByVal newLoad As clsLoad)
Set pMax = newLoad
UpdateMaxMin
End Property
'''''''''''''''''''''''''
' Min property
'''''''''''''''''''''''''
Public Property Get Min() As clsLoad
Set Min = pMin
UpdateMaxMin
End Property
Public Property Set Min(ByVal newLoad As clsLoad)
Set pMin = newLoad
UpdateMaxMin
End Property
'''''''''''''''''''''''''
' Occur property
'''''''''''''''''''''''''
Public Property Get Occ() As clsLoad
Set Occ = pOcc
End Property
Public Property Set Occ(ByVal newLoad As clsLoad)
Set pOcc = newLoad
UpdateMaxMin
End Property
'''''''''''''''''''''''''
' Source property
'''''''''''''''''''''''''
Public Property Get Source() As String
Source = pSource
End Property
Public Property Let Source(ByVal Val As String)
pSource = Val
End Property
'''''''''''''''''''''''''
' UpdateMaxMin sub - Swap max and min objects if max is less than min
'''''''''''''''''''''''''
Private Sub UpdateMaxMin()
Dim tmpLoad As clsLoad
If pMax.Value < pMin.Value Then
Set tmpLoad = pMax
Set pMax = pMin
Set pMin = tmpLoad
Debug.Print "Max less than min. Swapped max and min."
End If
End Sub