I\'m using an interface to ensure some similar classes implements some mandatory methods (subs/functions).
Example:
You could implement semi-decorator :-) pattern in VBA. Lets say we have a base class and two child classes. If you use Implements keyword in child classes you will garatee that child classes has the same interface as the base class and at the same time declare a private instance of base class in each child class and redirect calls from children to base class.
Note: base class here is normal class, you can still create instances of it (so it is not real abstract class).
Example:
' Standard module
Sub main()
Dim a As New ChildA
Dim b As New ChildB
a.State = 2
b.State = 5
Debug.Print TypeOf a Is Base
Debug.Print TypeOf b Is Base
TestPolymorphic a
TestPolymorphic b
End Sub
Private Sub TestPolymorphic(ByRef obj As Base)
obj.Polymorphic
End Sub
' -----------------------------------------------
' Base class module
Private m_state As Byte
Public Event StateChanged(oldState As Byte, newState As Byte)
Public Property Get State() As Byte
State = m_state
End Property
Public Property Let State(ByVal newState As Byte)
Dim oldState As Byte
oldState = m_state
m_state = newState
RaiseEvent StateChanged(oldState, newState)
End Property
Sub Polymorphic()
Err.Raise 123, , "Implement in child class"
End Sub
Private Sub Class_Initialize()
m_state = 1
End Sub
' -----------------------------------------------
' ChildA class module
Implements Base
Private WithEvents m_base As Base
Private Sub Class_Initialize()
Set m_base = New Base
End Sub
Public Property Get State() As Byte
State = Base_State
End Property
Public Property Let State(ByVal newState As Byte)
Base_State = newState
End Property
Public Sub Polymorphic()
Base_Polymorphic
End Sub
Private Property Get Base_State() As Byte
Base_State = m_base.State
End Property
Private Property Let Base_State(ByVal newState As Byte)
m_base.State = newState
End Property
Private Sub Base_Polymorphic()
Debug.Print "In Child A ..."
End Sub
Private Sub m_base_StateChanged(oldState As Byte, newState As Byte)
Debug.Print "State of 'Child A' instance has changed from " & oldState & " to " & newState
End Sub
Output:
' State of 'Child A' instance has changed from 1 to 2
' State of 'Child B' instance has changed from 1 to 5
' True
' True
' In Child A ...
' In Child B ...