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