tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,832
- Office Version
- 365
- 2019
- Platform
- Windows
I am trying to learn about class interfaces but don't appreciate them.
The first method uses it.
Method1:
' Standard Module1:
' Class IInterface
' Class ClsApples
' Class ClsOranges
Method2:
'Standard Module2:
' Class ClsApples
'Class ClsOranges
Method2 works without the need for the class IInterface, so why have it?
Can someone please explain.
Thanks
The first method uses it.
Method1:
' Standard Module1:
Code:
Option Explicit
Public Sub abc()
Dim a As IInterface
Dim i As Long
For i = 1 To 20
Set a = CFactory(i)
Sheet1.Cells(i, 2).Value = a.Fruit
Next i
End Sub
Public Function CFactory(i As Long) As IInterface
Dim b As IInterface
If Sheet1.Cells(i, 1).Value Mod 2 = 1 Then
Set b = New ClsApples
Else
Set b = New ClsOranges
End If
Set CFactory = b
End Function
' Class IInterface
Code:
Option Explicit
Public Function Fruit() As String
End Function
' Class ClsApples
Code:
Option Explicit
Implements IInterface
Public Function Iinterface_Fruit() As String
Iinterface_Fruit = "Apples"
End Function
' Class ClsOranges
Code:
Option Explicit
Implements IInterface
Public Function Iinterface_Fruit() As String
Iinterface_Fruit = "Oranges"
End Function
Method2:
'Standard Module2:
Code:
Option Explicit
Public Sub abc()
Dim i As Long
For i = 1 To 20
Call Module2.CFactory(i)
Next i
End Sub
Public Sub CFactory(i As Long)
Dim b As ClsApples
Dim c As ClsOranges
If Sheet1.Cells(i, 1).Value Mod 2 = 1 Then
Set b = New ClsApples
Sheet1.Cells(i, 2).Value = b.Fruit
Else
Set c = New ClsOranges
Sheet1.Cells(i, 2).Value = c.Fruit
End If
End Sub
' Class ClsApples
Code:
Option Explicit
Public Function Fruit() As String
Fruit = "Apples"
End Function
'Class ClsOranges
Code:
Option Explicit
Public Function Fruit() As String
Fruit = "Oranges"
End Function
Method2 works without the need for the class IInterface, so why have it?
Can someone please explain.
Thanks