Properties

These are the quantifiable characteristics of an object.
These are methods that allow you to read or write particular values associated with an object.
A large number of the properties are readonly.
Properties will often return other objects.
You can find out whether an exception will be generated by looking in the object model documentation.
Every object has properties.
Every object has a default property.
Property values can be Numerical, String or Boolean and they may be specific or apply to several objects.
They can be obtained or set by assignment statements.
Some properties cannot be set (ie read-only as opposed to read-write). Many properties have built in constants that should be used for their values.
You can easily use the NOT operator to toggle properties on or off. (eg ActiveWindow.DisplayAlerts = NOT ActiveWindow.DisplayAlerts).


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

Name

This variable is declared as Public and therefore automatically becomes a property of this class.


Hourly Rate

This variable is declared as Public and therefore automatically becomes a property 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.
Lets you return a property value or property reference
Take action before retrieving
Return a calculated value


Property Let - used to assign the value to a property of a class. Set the value of scalar property.
Lets you define a new property value
Take action after setting
Perform data validation


Property Set - used to assign an object to a property of a class. Set a reference for object property.
Lets you define a new property reference


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

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

Exit Property

This can be used to cause an immediate exit from the property
You can use any number in a property statement.


Property - Collection

Private mcolProperty_Name As Collection 

Property Get Property_Name() As Collection
    Set Property_Name = mcolProperty_Name
End Property

Property Set Property_Name(rData As Collection)
    Set mcolProperty_Name = rData
End Property

Property - Object

Private mobjProperty_Name As Object 

Property Get Property_Name() As Object
    Set Property_Name = mobjProperty_Name
End Property

Property Set Property_Name(rData As Object)
    Set mobjProperty_Name = rData
End Property

Property - Date

Private mdtProperty_Name As Date 

Property Get Property_Name() As Date
    Property_Name = mdtProperty_Name
End Property

Property Let Property_Name(rData As Date)
    mdtProperty_Name = rData
End Property

Property - Variant

Private mvarProperty_Name As Variant 

Property Get Property_Name() As Variant
    If IsObject(mvarProperty_Name) Then
        Set Property_Name = mvarProperty_Name
    Else
        Property_Name = mvarProperty_Name
    End If
End Property

Property Let Property_Name(rData As Variant)
    mvarProperty_Name = rData
End Property

Property Set Property_Name(rData As Variant)
    Set mvarProperty_Name = rData
End Property

Property - String

Private mstrPropertyName As String 

Property Get PropertyName() As String
    PropertyName = mstrPropertyName
End Property

Property Let PropertyName(rData As String)
    mstrPropertyName = rData
End Property

Important

If you are using an Object datatype then you must use Set instead of Let


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