Basic toMany Unidirectional Associations

If the Person gets rich then we might want to model the person owning many cars, as shown:

A basic implementation of this is to embed a collection of Cars into Persons and then allow client code to add Cars to Person at will.

Here is some rudimentary code from Person:

'private implementation of person Private m_myName As String

Private m_myTransport As New Cars

'return person's name

Public Property Get Name() As String

Name = m_myName End Property

'set person's name

Public Property Let Name(ByVal newName As String)

m_myName = newName End Property

Public Property Get Transport() As Cars

Set Transport = m_myTransport End Property

Note the embedded, hidden Cars collection in Person. This holds the references to the associated Car objects. The code for Car follows. (This was generated by the class builder add-in.)

The Cars collection code:

Option Explicit

'local variable to hold collection Private mCol As Collection

Public Function Add(Make As String, Owner As Person, Optional sKey As String)

As Car

'create a new object Dim objNewMember As Car Set objNewMember = New Car

'set the properties passed into the method objNewMember.Make = Make If IsObject(Owner) Then

Set objNewMember.Owner = Owner

Else objNewMember.Owner = Owner End If

If Len(sKey) = 0 Then mCol.Add objNewMember

Else mCol.Add objNewMember, sKey End If

'return the object created Set Add = objNewMember Set objNewMember = Nothing

End Function

Public Property Get Item(vntIndexKey As Variant) As Car 'used when referencing an element in the collection 'vntIndexKey contains either the Index or Key to the collection, 'this is why it is declared as a Variant 'Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5) Set Item = mCol(vntIndexKey) End Property

Public Property Get Count() As Long

'used when retrieving the number of elements in the 'collection. Syntax: Debug.Print x.Count Count = mCol.Count End Property

Public Sub Remove(vntIndexKey As Variant)

'used when removing an element from the collection 'vntIndexKey contains either the Index or Key, which is why 'it is declared as a Variant 'Syntax: x.Remove(xyz)

mCol.Remove vntIndexKey End Sub

Public Property Get NewEnum() As IUnknown 'this property allows you to enumerate 'this collection with the For...Each syntax Set NewEnum = mCol.[_NewEnum] End Property

Private Sub Class_Initialize()

'creates the collection when this class is created Set mCol = New Collection End Sub

Private Sub Class_Terminate()

'destroys collection when this class is terminated Set mCol = Nothing End Sub

To test the collection, we can write some code for the main application routine like this:

Sub Main()

'simple 1 to many unidirectional association

'1. create some objects to connect Dim aPerson As New Person aPerson.Name = "Russell"

Dim aFastCar As New Car aFastCar.Make = "Ferrari"

Dim aSlowCar As New Car aSlowCar.Make = "Reliant"

'2. associate the

cars

with the

person

aPerson.Transport.

Add

"'Ferrari",

, aPerson,

"R999

ICH"

aPerson.Transport.

Add

"'Reliant",

, aPerson,

"S111

LOW"

'3. print the makes of car the person

owns

Debug.Print aPerson.Name + " owns the

following car makes:

- "

Dim aCar As Car

For Each aCar In aPerson.Transport

Debug.Print "A " + aCar.Make

Next

End Sub

The example code assumes that the collection is not ordered in any way.

Building collection classes can be tedious so it is recommended that developers take a look at the Class Builder add-in, which provides collection-building features.

Was this article helpful?

0 0
365 Days Of Motivation

365 Days Of Motivation

Stop Wasting Time And Learn How To Stay Motivated. Finally! Discover How To Stop Your Mind From Wandering, And Upgrade Your Motivation. You Can Hack Your Motivation Levels, Allowing You To Take Your Life To The Next Level.

Get My Free Ebook


Post a comment