财产收集 [英] property collection
问题描述
我们有两节课。
头等舱:
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
我的问题是如何从HeaderInvoice类中获取价格以计算物业当地价格?
注意:我做的没有权限在Items 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?
推荐答案
如果LocalPrice依赖于R那么它应该是Item类的属性而不是Header
想一想。使用您的代码,您可以拥有类似的内容
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在集合中不知道它。 items不知道它在HeaderInvoice对象中。嗨不知道它有任何项目。
你可以通过让它实现IList并覆盖Add方法在HeaderInvoice对象中有一个速率,就像这样
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);
}
这也允许每个项目覆盖率更好的设计。
另外,如果你期待的话在计算中使用Price不应该是字符串。使用双。
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.
好的,你不喜欢最后的答案,所以试试这个。
使用代表,并将每个商品的价格与发票的费率一起传递给该代表。
我不打算给你提供示例代码,因为你显然似乎不需要它,我还有更好的事情要做。
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.
LocalPrice
属性如果不知道rate
,则没有任何意义。据你说,你不能把汇率
放入Item
类,所以你要么必须计算它发票
对象:
TheLocalPrice
property doesn't mean anything if it doesn't know therate
. According to you, you can't put therate
into theItem
class, so you either have to calculate it in theInvoice
object:
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
......或者你必须在 Item
类中创建一个计算它的方法根据提供给该方法的费率。
...or you have to create a method in the Item
class that calculates it based on the rate supplied to the method.
public class Item
public function CalcLocalPrice(rate as decimal) as decimal
return (me.Price * rate)
end function
end class
这篇关于财产收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!