0
Bonjour!計算屬性的型號
我有誰包含許多經典特性(代碼,描述,價(含稅),價格不含增值稅,增值稅)產品型號。對於我的例子,我只提出有趣的屬性。
在這個模型中,我想補充的價格計算。當任何量的變化,計算其他屬性(如果還原的變化,重新計算價(含稅)和價格不含增值稅,反之亦然)
我的例子:
public class Product : EditableObject
{
//VAT percentage propertie (ex: 20%)
private decimal _vat;
public decimal Vat
{
get { return _vat; }
set
{
_vat = value;
PriceWithVat = _priceWithoutVat * (1 + Vat/100); //Vat changed, we recalculate Price WITH VAT propertie
PriceWithoutVat = _priceWithVat/(1 + Vat/100); //Vat changed, we recalculate Price WITHOUT VAT propertie
NotifyOfPropertyChange(() => Vat);
}
}
//Product Price Without VAT
private decimal _priceWithoutVat;
public decimal PriceWithoutVat
{
get { return _priceWithoutVat; }
set
{
_priceWithoutVat = value;
PriceWithVat = _priceWithoutVat * (1 + Vat/100); //PriceWithoutVat changed, we recalculate Price WITH VAT propertie
NotifyOfPropertyChange(() => PriceWithoutVat);
}
}
//Product Price WITH Vat
private decimal _priceWithVat;
public decimal PriceWithVat
{
get { return _priceWithVat; }
set
{
_priceWithVat = value;
PriceWithoutVat = _priceWithVat/(1 + Vat/100); //PriceWithVat changed, we recalculate Price WITHOUT VAT propertie
NotifyOfPropertyChange(() => PriceWithVat);
}
}
}
由此,在任何價格變化,我有無限循環和堆棧溢出。正常的,因爲當任何價格的變動,所有其他被重新計算,並重新計算它們又將價格:)
你有解決方案,自動重新計算我的3種金額時,它們中的任何改變?
謝謝丹尼斯。但在你的例子中,如果我想分配PriceWithVat價格會發生什麼? WITHOUT沒有計算? – Damosse31
我希望我的用戶在我的應用程序中免費提供有或沒有增值稅的價格。 – Damosse31
它工作正常!謝謝丹尼斯 – Damosse31