Public Variables

Public variables declare the properties for the object.
When your properties are defined Public they are automatically available as properties of the class.
Any variables declared inside a class (even those declared as public) are not available outside of the class.
This is particularly relevant to Worksheet and Userform class modules.

Public Name As String 
Public HourlyRate As Double

Both these variables are declared as Public and therefore automatically become properties of this class.

Disadvantages of Public Variables

Class has no way of knowing if an outside process has changed the value.
There is no way of performing any data validation.
There is no way of creating read-only or write-only properties.

Alternative to Public Variables

The alternative is to use property procedures instead.
Property Get - used to retrieve a property of a class. Get the value of scalar of object property.
Property Let - used to assign the value to a property of a class. Set the value of scalar property.
Property Set - used to assign an object to a property of a class. Set a reference for object property.

When your properties (or variables) are defined Private they should only be modified from within the Property Get and Property Let procedures.

Private msDepartment As String 
Private mdbNormalHours As Double
Private mdbOvertimeHours As Double

© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopPrevNext