Private vs Public class

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,926
Office Version
  1. 365
  2. 2019
Platform
  1. 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:

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
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
it still works.

Can someone please explain why it doesn't seem to matter, whether Sub ClsInterface_Calc() is declared Public or Private?
I am not sure what you mean when you say it still works--I do not see it called in any of the code you presented. How is it being used?
 
Upvote 0
I am not sure what you mean when you say it still works--I do not see it called in any of the code you presented. How is it being used?
Copy the code and run it.

You'll see regardless of Private or Public, both versions will run and give the same result.

I didn't expect Private to compile.
 
Upvote 0
That's because your class ClsCalc implements the interface ClsInterface, in which the method Calc() is declared as Public. And so it simply "inherits" its properties. Now if you declare it as Private in ClsInterface and Public in ClsCalc, you'll see that you'll get an error.

[ClsInterface]

VBA Code:
Option Explicit

Private Sub Calc()
  
End Sub

[ClsCalc]

VBA Code:
Option Explicit

    Implements ClsInterface
 
Public Sub ClsInterface_Calc()

    Cells(1, 1).Value = 10
    
End Sub

Hope this helps!
 
Upvote 0
That's because your class ClsCalc implements the interface ClsInterface, in which the method Calc() is declared as Public. And so it simply "inherits" its properties. Now if you declare it as Private in ClsInterface and Public in ClsCalc, you'll see that you'll get an error.

[ClsInterface]

VBA Code:
Option Explicit

Private Sub Calc()
 
End Sub

[ClsCalc]

VBA Code:
Option Explicit

    Implements ClsInterface
 
Public Sub ClsInterface_Calc()

    Cells(1, 1).Value = 10
   
End Sub

Hope this helps!
Thanks for the explanation.

Given any class which implements the interface class, (ie ClsCalc in this example), it seems despite ClsCalc must contain all the methods and properties of ClsInterface, the scope need not be exactly the same.
 
Upvote 0
Well, in any case, I would ensure that they match, at least as a matter of consistency . . . :)
 
Upvote 0
Well, in any case, I would ensure that they match, at least as a matter of consistency . . . :)
I agree but was curious because I have never come across any books or websites which declares a Property As Private.

The compiler compiles, so obviously it's possible but I can't think of a situation where it would make sense.

If a Property was Private, it would mean anything outside of the class would not be able to set or retrieve its value, so it must be done internally by another Procedure within the class.
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top