tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,926
- Office Version
- 365
- 2019
- Platform
- Windows
VBA lacks Inheritence but Composition is usually preferred.
To my limited knowledge, Class interfaces are used "to promote" polymorphism in VBA.
This article shows composition:
Here is some of the code:
and this is a derived class:
What I don't understand is couldn't the above be replicated using class interfaces, ie:
and
with the interface:
If so, which method would be preferable, composition or interfaces?
Thanks
To my limited knowledge, Class interfaces are used "to promote" polymorphism in VBA.
This article shows composition:
Code:
https://www.microsoftpressstore.com/articles/article.aspx?p=2225071&seqNum=2
Here is some of the code:
Code:
' ClsPolicy
Dim p_MonthlyPremium As Currency
Public Property Get MonthlyPremium() As Currency
MonthlyPremium = p_MonthlyPremium
End Property
Public Property Let MonthlyPremium(ByVal MonthlyPremium As Currency)
p_MonthlyPremium = MonthlyPremium
End Property
Public Function CalculateAnnualPolicyValue() As Currency
CalculateAnnualPolicyValue = p_MonthlyPremium * 12
End Function
and this is a derived class:
Code:
' ClsHomePolicy
Dim p_Policy As clsPolicy
Private Sub Class_Initialize()
Set p_Policy = New clsPolicy
End Sub
Private Sub Class_Terminate()
Set p_Policy = Nothing
End Sub
Public Property Get MonthlyPremium() As Currency
MonthlyPremium = p_Policy.MonthlyPremium
End Property
Public Property Let MonthlyPremium(ByVal MonthlyPremium As Currency)
p_Policy.MonthlyPremium = MonthlyPremium
End Property
Public Function CalculateAnnualPolicyValue() As Currency
CalculateAnnualPolicyValue = p_Policy.CalculateAnnualPolicyValue() + 50
End Function
What I don't understand is couldn't the above be replicated using class interfaces, ie:
Code:
Implements ClsInterface
Dim p_MonthlyPremium As Currency
Public Property Get ClsInterface_MonthlyPremium() As Currency
ClsInterface_MonthlyPremium = p_MonthlyPremium
End Property
Public Property Let ClsInterface_MonthlyPremium(ByVal MonthlyPremium As Currency)
p_MonthlyPremium = MonthlyPremium
End Property
Public Function ClsInterface_CalculateAnnualPolicyValue() As Currency
ClsInterface_CalculateAnnualPolicyValue = p_MonthlyPremium * 12
End Function
and
Code:
Implements ClsInterface
Dim p_MonthlyPremium As Currency
Public Property Get ClsInterface_MonthlyPremium() As Currency
ClsInterface_MonthlyPremium = p_MonthlyPremium
End Property
Public Property Let ClsInterface_MonthlyPremium(ByVal MonthlyPremium As Currency)
p_MonthlyPremium = MonthlyPremium
End Property
Public Function ClsInterface_CalculateAnnualPolicyValue() As Currency
ClsInterface_CalculateAnnualPolicyValue = p_MonthlyPremium * 12 + 50
End Function
with the interface:
Code:
Public Property Get MonthlyPremium() As Currency
End Property
Public Property Let MonthlyPremium(ByVal MonthlyPremium As Currency)
End Property
Public Function CalculateAnnualPolicyValue() As Currency
End Function
If so, which method would be preferable, composition or interfaces?
Thanks