John.P
John.P

Reputation: 657

Adding a property to Add() method

I'm trying to add a property to a Add() method but it doesn't seem to work. I'm sure I'm missing something.

  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 class:

  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
                {
                }
            } 
        }

I'm getting two errors:

Invalid initializer member declarator

The name 'Total' does not exist in the current context

What I want the Total property to do is to add the result of Quantity * UnitPrice to the Add() method

Upvotes: 0

Views: 81

Answers (2)

Jacek
Jacek

Reputation: 12053

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

change to

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

you can use property with only getter

Upvotes: 2

Yuriy Zaletskyy
Yuriy Zaletskyy

Reputation: 5151

You do not have setter implemented for Total, but try to set some value for it. So I propose to either implement setter or remove usage of Total in initialization logic

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

Upvotes: 0

Related Questions