tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,926
- Office Version
- 365
- 2019
- Platform
- Windows
When starting off with classes, it is often stated that instead of using Public variables, use Public Properties because it gives you more control.
I have the following code in Class1:
To use it, I have this in a standard module:
Everything works as expected.
However, if I use an Interface and a class factory, this is ClsInterface:
This is ClsSettings:
This is ClsCalc:
In the standard module, I have this:
what I find puzzling is even if I changed the PUBLIC to PRIVATE, as in:
to:
it still works.
Can someone please explain why it doesn't seem to matter, whether Sub ClsInterface_Calc() is declared Public or Private?
Furthermore, are there any occasions where one SHOULD declare a Property as Private (ie when you don't want it exposed outside of the class)?
Thanks
I have the following code in Class1:
Code:
Option Explicit
Public Sub Calc()
Cells(1, 1).Value = 10
End Sub
To use it, I have this in a standard module:
Code:
Public Sub Basic
Dim abc As Class1
Set abc = New Class1
Call abc.Calc
Set abc = Nothing
End Sub
Everything works as expected.
However, if I use an Interface and a class factory, this is ClsInterface:
Code:
Option Explicit
Public Sub Calc()
End Sub
This is ClsSettings:
Code:
Option Explicit
Private Cls As ClsInterface
Public Sub Settings()
Set Cls = ClassFactory
Call Cls.Calc
End Sub
Private Function ClassFactory() As ClsInterface
Set ClassFactory = New ClsCalc
End Function
This is ClsCalc:
Code:
Option Explicit
Implements ClsInterface
Public Sub ClsInterface_Calc()
Cells(1, 1).Value = 10
End Sub
In the standard module, I have this:
Code:
Public Sub UsingClassFactory()
Dim abc As ClsSettings
Set abc = New ClsSettings
Call abc.Settings
Set abc = Nothing
End Sub
what I find puzzling is even if I changed the PUBLIC to PRIVATE, as in:
Code:
Option Explicit
Implements ClsInterface
PUBLIC Sub ClsInterface_Calc()
Cells(1, 1).Value = 10
End Sub
to:
Code:
Option Explicit
Implements ClsInterface
PRIVATE Sub ClsInterface_Calc()
Cells(1, 1).Value = 10
End Sub
it still works.
Can someone please explain why it doesn't seem to matter, whether Sub ClsInterface_Calc() is declared Public or Private?
Furthermore, are there any occasions where one SHOULD declare a Property as Private (ie when you don't want it exposed outside of the class)?
Thanks