涉及其他变量的属性

时间:2015-03-05 18:19:34

标签: c# properties

我该如何解决这个问题? City是我项目中的另一个属性/变量。 产品总数也是如此。 我试图根据销售的地点获得不同的销售税计算。

public double SalesTax  // Define result property
{
  get
  {
    if (City == "Town #1")
      return Math.Round(this.ProductTotal * #1_TAX_RATE, 2);
    else if (City == "Town #2")
      return Math.Round(this.ProductTotal * #2_TAX_RATE, 2);
    else if (City == "Town #3")
      return Math.Round(this.ProductTotal * #3_TAX_RATE, 2);
    else if (City == "Town #4")
      return Math.Round(this.ProductTotal * #4_TAX_RATE, 2);
  }
  set
  {
    if ((value >= 0) && (value <= 1000.0))
      this.SalesTax = value;
    else
      ProcessError(String.Format("(0) can not be assigned to a SalesTax property", value));
  }
}

指出的错误是get没有返回值,我不明白为什么?

2 个答案:

答案 0 :(得分:1)

此属性应该是只读的,如果是,还需要返回一些内容 城市=其他东西

public double SalesTax  // Define result property
{
  get
  {
    int saleRate = 1;
    if (City == "Town #1")
      saleRate = #1_TAX_RATE;
    else if (City == "Town #2")
      saleRate = #2_TAX_RATE;
    else if (City == "Town #3")
      saleRate = #3_TAX_RATE;
    else if (City == "Town #4")
      saleRate = #4_TAX_RATE;

    return Math.Round(this.ProductTotal * saleRate, 2);
  }
}

答案 1 :(得分:1)

尝试更改你的获取:

get
  {
    switch(City)
    {
       case "Town #1":
            return Math.Round(this.ProductTotal * #1_TAX_RATE, 2);
       break;

      case "Town #2":
           return Math.Round(this.ProductTotal * #2_TAX_RATE, 2);
       break;

      case "Town #3":
           return Math.Round(this.ProductTotal * #3_TAX_RATE, 2);
       break;

     case "Town #4":
           return Math.Round(this.ProductTotal * #4_TAX_RATE, 2);
       break;

     case default:
           //default return value
       break;
    }