Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
We have two classes.

first class:
Public Class HeaderInvoice

    Dim _ID As String
    Dim _Rate As Decimal
    Dim _Items As List(Of Item)

    Property ID() As Integer
        Get
            Return _ID
        End Get
        Set(ByVal value As Integer)
            _ID = value
        End Set
    End Property

    Property Rate() As Decimal
        Get
            Return _Rate
        End Get
        Set(ByVal value As Decimal)
            _Rate = value
        End Set
    End Property


    Property Items() As List(Of Item)
        Get
            Return _Items
        End Get
        Set(ByVal value As List(Of Item))
            _Items = value
        End Set
    End Property

End Class


second class:
Public Class Item

    Dim _Id As Integer
    Dim _Price As Decimal

    Property ID() As Integer
        Get
            Return _Id
        End Get
        Set(ByVal value As Integer)
            _Id = value
        End Set
    End Property

    Property Price() As String
        Get
            Return _Price
        End Get
        Set(ByVal value As String)
            _Price = value
        End Set
    End Property

    ReadOnly Property LocalPrice() As Decimal
       Get
***********
       End Get

    End Property
End Class


My question is how to get the value of rate from class HeaderInvoice to calculate the property local price?
Note: I do not have permission to insert property rate in Items Class.
Can anyone can help me?
Posted
Updated 25-Nov-10 3:09am
v2
Comments
fjdiewornncalwe 25-Nov-10 8:02am    
OK.. You have shown us your portions of your class definitions and that is great, but we need to know you you are implementing the HeaderInvoice and Item classes in order to explain how to do what you want to do.
#realJSOP 29-Nov-10 7:56am    
Why did you low-vote the answers supplied? You asked for a solution, and you got two viable ones from highly reputable users. This is not going to convince others to help you in the future.

If the LocalPrice is dependent on Rate then it should be a property of the Item class not the Header


Think about it a little. With your code you can have something like this
HeaderInvoice hi = new HaderInvoice();
List<Item> items = new List<Item>();
Item item1 = new Item();
item1.ID = 1;
item1.Price = "5.00";
items.Add(item1);
hi.Items = items;

You are expecting to be able to do this.
Decimal rate = hi.Items[0].LocalRate;

item1 doesn't know it in a collection. items doesn't know it is in a HeaderInvoice object. And hi doesn't know that it has any items.
You can still have one rate in the HeaderInvoice object by having it implement IList and override the Add method, something like this
private List<Item> m_Items;
public override int Add(Item item)
{
   if(item.Rate == 0)
   {
     item.Rate = this.Rate;
   }
   m_Items.Add(item);
}

This also allows for each item to override the rate which is a better design.
Also, if you are expecting to use Price in a calculation it should not be a string. Use a double.
 
Share this answer
 
v2
Comments
MustafaJuma 27-Nov-10 5:19am    
thank you for interest .
suppose with me ,
That we have one invoice that contains 100 items
Therefore, will have 100 rate While there is supposed to be a single rate for each invoice
[no name] 27-Nov-10 8:36am    
Edited answer to address this
Okay, you didn't like the last answer, so try this one.

Use a delegate, and just pass in each items price along with the invoice's rate to that delegate.

I'm not going to bother giving you sample code because you obviously don't seem to need it, and I have better things to do.
 
Share this answer
 
v2
The LocalPrice property doesn't mean anything if it doesn't know the rate. According to you, you can't put the rate into the Item class, so you either have to calculate it in the Invoice object:

VB
public class HeaderInvoice
    public function CalcLocalPrice(index as integer) as decimal
        dim localPrice as decimal = 0
        if (index >= 0 AndAlso index < me.Count) then
            loaclPrice = me[index].Price * rate)
        end if
        return localPrice
    end function
end class


...or you have to create a method in the Item class that calculates it based on the rate supplied to the method.

VB
public class Item
    public function CalcLocalPrice(rate as decimal) as decimal
        return (me.Price * rate)
    end function
end class



 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900