This seems like there is a really basic answer...
I have a collection of objects (lets say Products) inside another object (say Order). The product object has a function called GetTax(). Inside that function it needs to reference a property of its parent object, Order. How can I access a property of a containing object In the below sample, the MyParent object is pseudo-code...
Imports System.Collections.Generic Public Class Order Public Class Product |
Do I need a reference object to the Order I'm missing something.

Object Oriented question
tj01
*Rick*
Class Product
Private _order As Order
Public Property Order As Order
Get
return _order
End Get
Set
_order = Value
End Set
End Property
public function GetTax() as Double
If _order Is Nothing Then
Throw new Exception("No order")
End If
Return _order.GetTax()
End Function
End Class
bschaldon
I mean Dependency Inversion
distef01
Dede
Savannah
Perhaps what you could consider is to have the GetTax method accept an argument that specifies a payment type. This could be another object but more simply could be an enumeration. Then, if you needed the total tax for the order, simply iterate throught the contained products, using the payment method enumeration in the method call to sum the tax and return a total.
I don't know how clear that is, so here's my idea in code
Imports System.Collections.Generic
Public Enum PaymentMethod
Cash = 1,
CreditCard = 2,
Cheque = 3
End Enum
Public Class Order
Public PaymentMethod As PaymentMethod = PaymentMethod.Cash
Public Products As List(Of Product)
Public Function GetTax() as Double
Dim dTotalTax as Double = 0
For Each Product in Products
dTotalTax += Product.GetTax(Me.PaymentMethod)
End For
Return dTotalTax
End Function
End Class
Public Class Product
Public Function GetTax(ByVal payMethod as PaymentMethod) As Decimal
If payMethod = PaymentMethod.Cash Then
Return 0
Else
Return 0.1
End If
End Function
End Class
wesam