
Chapter 6: Debugging with Visual Studio | 147 |
VB: Customer.vb
Class Customer
Property Discount As Decimal
Property Order As Order
Function GetOrderDiscount() As Decimal
Return Order.Total * Discount
End Function
End Class
VB: Order.vb
Class Order
Private orderItems As New List(Of Decimal)
Public ReadOnly Property Total() As Decimal
Get
Dim amount As Decimal = 0
For Each item In orderItems amount = amount + item
Next
Return amount
End Get
End Property
Sub AddItem(ByVal item As Decimal) orderItems.Add(item)
End Sub
End Class
A quick look at the code in Listing
After instantiating Customer, you can see that the Discount property on cust is being set to .1 (10%). This means that each instance of Customer can have a unique discount amount, which could be useful if you wanted to reward good shopping habits.
Next, you can see the instantiation of Order and subsequent calls to AddItem on the object reference ord. This code only adds the order amount, but in a real scenario it would likely be a class with more fields to carry the specific details of the order item. The Customer class has an Order property, which the code then passes our Order instance, ord, to. Now, you have a Customer with a discount amount and it has a reference to our specific Order, which in turn has items (represented here by the items’ monetary amount only for brevity).