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".
Properties (and objects of classes) are created and interfaced by two ways:
'This variable can be accessed from outside of the object
Public mintX1 as Integer
'The variable cannot Private mintX2 as Integer
By judicious use of combinations of Get, Let, and/or Set, the property can be read-only, write-only, or read- write.
Private mintHeight As Integer
Public Property Get Height() As Integer Height = mintHeight End Property
Private mintHeight As Integer
Public Property Let Height(ByVal intHeight As Integer) mintHeight = intHeight End Property
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
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
One of the more common properties to set is a Parent property so that you can work up and down an object tree.
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
Dim mobjJunior As Junior
Set mobjJunior = New Junior Set mobjJunior.Parent = Me
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
obj = 9 is the same as obj.Value = 9 if Value is the default property of that object.Page Modified: (Hand noted: 2007-09-20 21:31:12Z) (Auto noted: 2007-11-17 06:36:57Z)