Object Oriented question

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 PaymentMethod As String
    Public Products As List(Of Product)

    Public Class Product
        Public Function GetTax() As Decimal
            If MyParent.PaymentMethod = "Cash" Then  
                Return 0
            Else
                Return 0.1
            End If
        End Function
    End Class
End Class

 



Do I need a reference object to the Order   I'm missing something.



Answer this question

Object Oriented question

  • tj01

    Can you tell me how to do this   Do I pass it as a reference in the constructor   A few lines of code por favor.
  • *Rick*

    I would create a property in the Product class to insert your order and if you ask for the tax of a product and there is no order, throw an exception. Cause if you have a product, it does not necesarely mean you have an order.

    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

     slabanum wrote:
    Really liked your solution, you decoupled both product and order classes and have them look at the PaymentMethod instead.  Inversion Of Control principle baby!



    I mean Dependency Inversion

  • distef01

    Yes, your Product needs to have a reference to the Order object that contains it.
  • Dede

    Really liked your solution, you decoupled both product and order classes and have them look at the PaymentMethod instead.  Inversion Of Control principle baby!


  • Savannah

    I think you need to reconsider how you're doing this. The idea of nesting the order in the product makes me feel a bit irky, and looks a little circular for my liking Big Smile 

    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 Big Smile




    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

    Thanks slabanum Big Smile


  • Object Oriented question