Building Hierarchies of Classes
It is common to emulate real-life relationships between objects in the classes that you build. This necessitates the building of a class hierarchy. The relationships that you build between classes make up an object model. For example, you might have a Client class that has multiple Order objects associated with it. Each individual Order object can then have multiple Order Detail objects associated with it.To relate one class to another, place a declaration of the child class in the General Declarations section of the parent. For example, the Order class contains the following declaration:Public OrderDetail as OrderDetail
The Initialize event of the Order class contains the code that instantiates the OrderDetail class:Private Sub Class_Initialize()
Set OrderDetail = New OrderDetail
End Sub
When you instantiate the Order class (the parent class), the code automatically instantiates the child class. You can then set the properties and execute the methods of the child class. Here's an example:Sub CreateOrder()
'Declare and instantiate the Order object
Dim objOrder As Order
Set objOrder = New Order
'Set properties of the child class (OrderDetail)
With objOrder.OrderDetail
.ItemNumber = 5
.OrderNumber = 1
.Quantity = 3
End With
End Sub
Notice that the code declares and instantiates an Order object. It then uses a With statement to point at the OrderDetail object instantiated in the Initialize event of the Order class. It sets the ItemNumber, OrderNumber, and Quantity properties of the OrderDetail object.This example shows how to have one child associated with a parent. The section titled "Using a Collection to Manipulate Multiple Instances of the FileInformation Class" shows how to use a Custom collection to emulate a one-to-many relationship with classes.