tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,924
- Office Version
- 365
- 2019
- Platform
- Windows
This video demonstrates how to use a class interface:
and this is the code:
Instead of using interfaces, why not just have a single class that takes a single parameter, either A, B or C.
Then within that class, write some If statements.
So for example:
This is the standard module:
My alternative idea produces the same results, so what advantages would using interfaces bring or is it all about polymorphism?
Thanks
Code:
https://www.youtube.com/watch?v=3bO50gHRndA
and this is the code:
Code:
Option Explicit
' The Interface Class - this is implemented by clsInterestA, clsInterestB and clsInterestC
Sub Calculate(ByVal amount As Double)
End Sub
Sub PrintResult()
End Sub
' Class clsInterestA
Implements iInterest
Private m_Amount As Double
' Calculate the interest
Sub iInterest_Calculate(ByVal amount As Double)
m_Amount = amount * 1.1
End Sub
' Print the result to the Immediate Window
Sub iInterest_PrintResult()
Debug.Print TypeName(Me) & ": " & m_Amount
End Sub
' Class clsInterestB
Implements iInterest
Private m_Amount As Double
' Calculate the interest
Sub iInterest_Calculate(ByVal amount As Double)
m_Amount = amount * 1.5
End Sub
' Print the result to the Immediate Window
Sub iInterest_PrintResult()
Debug.Print TypeName(Me) & ": " & m_Amount
End Sub
' Class clsInterstC
Implements iInterest
Private m_Amount As Double
' Calculate the interest
Sub iInterest_Calculate(ByVal amount As Double)
m_Amount = amount + 1000
End Sub
' Print the result to the Immediate Window
Sub iInterest_PrintResult()
Debug.Print TypeName(Me) & ": " & m_Amount
End Sub
]/code]
This is in a standard module:
[code]
Option Explicit
Sub Main()
' Get the range of data
Dim rg As Range
Set rg = shData.Range("A1").CurrentRegion
' Declare the interface variable - this can reference any class
' that implements iInterest.
Dim oInterest As iInterest
' data variables
Dim amount As Double, interestType As String
' Read through the data
Dim i As Long, result As Double
For i = 2 To rg.Rows.Count
' read the current row to variables
amount = rg.Cells(i, 1).Value
interestType = rg.Cells(i, 2).Value
' Get the interest object
Set oInterest = ClassFactory(interestType)
' Calculate the interest
oInterest.Calculate amount
' some code
' Print the interest to the Immediate Window
oInterest.PrintResult
Next i
End Sub
Function ClassFactory(ByVal interestType As String) As iInterest
Dim oInterest As iInterest
If interestType = "A" Then
Set oInterest = New clsInterestA
ElseIf interestType = "B" Then
Set oInterest = New clsInterestB
ElseIf interestType = "C" Then
Set oInterest = New clsInterestC
Else
MsgBox "Invalid type " & interestType
End If
Set ClassFactory = oInterest
End Function
Instead of using interfaces, why not just have a single class that takes a single parameter, either A, B or C.
Then within that class, write some If statements.
So for example:
Code:
' Single class
Option Explicit
Private pInterestType As String
Private m_Amount As Double
Public Property Get InterestType() As String
InterestType = pInterestType
End Property
Public Property Let InterestType(ByVal I As String)
pInterestType = I
End Property
' Calculate the interest
Sub Calculate(ByVal amount As Double)
Select Case InterestType
Case "A"
m_Amount = amount * 1.1
Case "B"
m_Amount = amount * 1.5
Case "C"
m_Amount = amount + 1000
End Select
End Sub
' Print the result to the Immediate Window
Sub PrintResult()
Debug.Print TypeName(Me) & ": " & m_Amount
End Sub
This is the standard module:
Code:
Option Explicit
Sub Main()
' Get the range of data
Dim rg As Range
Set rg = shData.Range("A1").CurrentRegion
Dim oInterest As Class1
' data variables
Dim amount As Double, InterestType As String
' Read through the data
Dim I As Long, result As Double
For I = 2 To rg.Rows.Count
' read the current row to variables
amount = rg.Cells(I, 1).Value
InterestType = rg.Cells(I, 2).Value
Set oInterest = New Class1
oInterest.InterestType = InterestType
' Calculate the interest
oInterest.Calculate amount
' some code
' Print the interest to the Immediate Window
oInterest.PrintResult
Next I
End Sub
My alternative idea produces the same results, so what advantages would using interfaces bring or is it all about polymorphism?
Thanks