Properties are changeable characteristics of an object. This includes variables, settings, attributes, and data. Properties are usually named as nouns or adjectives. EG: "Height" or "Visible".

Intro

Properties (and objects of classes) are created and interfaced by two ways:

  1. Declare a Public variable in a Form or Class module. A private variable cannot provide the data protection and validation that Property procedures can. Note that if the variable were declared as Private or just as Dim then, its scope would be just within the module itself. EG:
    'This variable can be accessed from outside of the object
    Public  mintX1 as Integer
    'The variable cannot Private mintX2 as Integer  
  2. Utilize Property procedures:
    • Retrieve from the object:
      • Public Property Get procedures pass data from inside the object to outside the object, i.e. it lets a user of an object of the class to read the property. Note that it is rare but possible for the Property Get() sub to accept arguments.
    • Pass into the object:
      • Public Property Let procedures pass scalar values (eg strings) from outside the object to inside the object, i.e. it lets a user of an object of the class to let the property be a particular value.
      • Public Property Set procedures pass an object from outside the object to inside the object, i.e. it lets a user of an object of the class to set the object variable to a particular object.

By judicious use of combinations of Get, Let, and/or Set, the property can be read-only, write-only, or read- write. 

Examples

Read-Only

Private mintHeight As Integer
Public Property Get Height() As Integer
   Height = mintHeight
End Property

Write-Only

Private mintHeight As Integer
Public Property Let Height(ByVal intHeight As Integer)
  mintHeight = intHeight 
End Property

Read-Write

Private mintHeight As Integer
Public Property Get Height() As Integer
   Height = mintHeight
End Property
Public Property Let Height(ByVal intHeight As Integer)
  mintHeight = intHeight 
End Property

Read-Write Variant

Here is an example of a property with a variant data type. Get, Let, and Set must all be used.

Private mvntTest as Variant
Public Property Get Test() As Variant
  If IsObject(mvntTest) Then
    Set Test = mvntTest
  Else
    Test = mvntTest
  End If
End Property
Public Property Let Test(ByVal NewTest As Object)
  mvntTest = NewTest
End Property
Public Property Set Test(ByVal NewTest As Object)
  Set mvntTest = NewTest
End Property

Parent-Child

One of the more common properties to set is a Parent property so that you can work up and down an object tree.

  1. Make code like this in the child class module "Junior" to make Parent a write-once property:
    Private mobjParent As Object
    Property Set Parent(objParent As Object)
      If mobjParent Is Nothing Then
        Set mobjParent = objParent
      End If
    End Property
    Property Get Parent() As Object
      Set Parent = mobjParent
    End Property
  2. Use code like the following when code in the parent class module "Senior" instantiates "Junior".
    Dim mobjJunior As Junior
    Set mobjJunior = New Junior
    Set mobjJunior.Parent = Me

Collection

The usual members of a collection (Count, Add(), Item(), and Remove()) must be created if you are letting your class have its own collection. In this example, a class ("Letter") has a collection ("Letters").

Private mcolLetters As New Collection
'A read-only property
Property Get Count() As Long
  Count = mcolLetters.Count
End Property
'A method of this class
Public Sub Add(ByVal strValue As Integer, Optional ByVal varBeforeOrAfter)
  Dim objLetter As New Letter
  'Any number of properties could have been set for the class
  objLetter.Value = strValue As Integer
  'This assumes that the Letter class automatically makes a new ID upon instantiation
  mcolLetters.Add objLetter, objLetter.ID, varBeforeOrAfter
End Sub
'A property/method of this class
Property Get Item(ByVal varID As Variant) As Letter
  Set Item = mcolLetters(varID)
End Property
Public Function Letters(Optional varID As Variant) As Object
  If IsMissing(varID) Then
    Set Letters = mcolLetters
  Else
    Set Letters = mcolLetters.Item(varID)
  End If
End Function
'A method of this class
Public Sub Remove(ByVal varID as Variant)
  mcolLetters.Remove varID
End Sub
'You could add methods/properties to the collection that are not standard! Property Let Changed(ByVal blnChanged As Boolean)
  Dim objLetter As Letter
  For Each objLetter in mcolLetters
    objLetter.Changed = blnChanged
  Next
End Property
Property Get Changed() As Boolean
  Dim objLetter As Letter
  For Each objLetter in mcolLetters
    If objLetter.Changed Then
      Changed = True
      Exit For
    End If
  Next
End Property

Note that a For...Each loop will not work on this custom collection. A For...Next loop would have to be used instead.

Just for show, here is how the Letter class might have automatically made a new ID upon instantiation:

Private Sub Class_Initialize()
  strID = CLng(Rnd * (2 ^ 31))
End Sub

Miscellany

Page Modified: (Hand noted: ) (Auto noted: )