向Add()方法添加属性

时间:2015-04-27 11:02:20

标签: c#

我正在尝试向Add()方法添加属性,但它似乎不起作用。我确定我错过了什么。

  Items.Add(new ItemProperties
                    {
                        Item = Convert.ToInt32(lines[i]),
                        Description = lines[i + 1],
                        Quantity = Convert.ToInt32(lines[i + 2]),
                        UnitPrice = Convert.ToInt32(lines[i + 3]),
                        Tax = Convert.ToInt32(lines[i + 4]),
                        TotalTax = 34234,
                        Total //<-- Error: Invalid initializer member declarator 

                    });

ItemProperties上课:

  public class ItemProperties
        {
            public int Item { get; set; }
            public string Description { get; set; }
            public int Quantity { get; set; }
            public int UnitPrice { get; set; }
            public int Tax { get; set; }
            public int TotalTax { get; set; }
            public int Total {
                get
                {
                    return Quantity * UnitPrice;
                } 
                set
                {
                }
            } 
        }

我遇到两个错误:

  

无效的初始化成员声明符

     

当前上下文中不存在名称“总计”

我希望Total属性执行的操作是将Quantity * UnitPrice的结果添加到Add()方法

2 个答案:

答案 0 :(得分:2)

public int Total 
{
    get
    {
        return Quantity * UnitPrice;
    } 
    set
    {
    }
} 

更改为

public int Total 
{
    get
    {
        return Quantity * UnitPrice;
    } 
} 

你可以使用只有getter的属性

答案 1 :(得分:0)

您没有为Total实施setter,但尝试为其设置一些值。所以我建议在初始化逻辑中实现setter或删除Total的使用

public int Total {
                get
                {
                    return Quantity * UnitPrice;
                } 
                set
                {
                   // you need to add some code
                }