fibershot.
fibershot.

Reputation: 81

Am I using the set/get function wrong?

There is this problem which I need to solve in my program which is about the get/set function.

HERE IS THE FULL UNEDITED CODE: https://pastebin.com/Vd8zC51m

EDIT: My good friend found the solution, which was to use "value" like so:

        {
            get
            {
                return this.returnValue;
            }

            set
            {
                if (value > 30)
                {
                    this.returnValue = value - (value * 0.10f);
                } else
                {
                    this.returnValue = value;
                }
            }
        }

I'M SORRY IF I WASTED ANYONES TIME...!

The problem goes like this: Make a Price attribute which returns the price reduced by 10% if it's over 30 using set/get.

What I'm getting: Value of 0.

I know what's wrong, I just don't know how to fix it. I know it's in the set command. After I show the code, you might have a better understanding.

I've tried searching around the internet how to use the set command correctly and tried different ways of doing the set command.

public class Book
{
    public string Name;
    public string writerName;
    public string bPublisher;
    public float bPrice;
    public string bTheme;
    public float returnValue;

    public Book(string name, string writer, string publisher, float price, string theme)
    {
        Name = name;
        writerName = writer;
        bPublisher = publisher;
        bPrice = price;
        bTheme = theme;
    }

    public float Price
    {
        get
        {
            return returnValue;
        }

        set
        {
            if (this.bPrice > 30)
            {
                returnValue = this.bPrice - (this.bPrice * 0.10f);
            } else
            {
                returnValue = this.bPrice;
            }
        }
    }
}

----------These are the main parts cut from the program----------------------

static void Main(string[] args)
{
    Book k2 = new Book("A book", "O. Writer", "Publisher Ab", 36.90f, "Fantasy");
    Console.WriteLine(k2.Price);
}

Upvotes: 6

Views: 167

Answers (4)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186688

So we have two prices here: net (e.g. 45.00) and reduced price (45.00 - 4.50 == 41.50)

public Book {
  ...
  const Decimal PriceThreshold = 30.0m;
  const Decimal ReducePerCent = 10.0m; 

  private Decimal m_NetPrice;

  // Net price
  // Decimal (not Single, Double) usually is a better choice for finance
  public Decimal NetPrice {
    get {
      return m_NetPrice;
    }
    set {
      if (value < 0) 
        throw new ArgumentOutOfRangeException(nameof(value));

      m_NetPrice = value;
    }
  }  

  // Price with possible reduction
  public Decimal Price {
    get {
      return NetPrice > PriceThreshold 
        ? NetPrice - NetPrice / 100.0m * ReducePerCent
        : NetPrice;
    } 
  } 
}

Please, note that we don't have set for Price property; there's ambiguity since one Price, say, 28.80 corresponds to two valid NetPrices (28.80 or 32.00: 32.00 - 3.20 == 28.80)

Upvotes: 7

Suhas Kulkarni
Suhas Kulkarni

Reputation: 263

The thing is you need to set the Price in the Book constructor as below:

public Book(string name, string writer, string publisher, float price, string theme)
    {
        Name = name;
        writerName = writer;
        bPublisher = publisher;
        Price = price; 
        bTheme = theme;
    }

And then in the setter of the Price you can do it like this:

public float Price
    {
        get
        {
            return returnValue;
        }

        set
        {
            if (value > 30)
            {
                returnValue = value - (value * 0.10f);
            }
            else
            {
                palautus = value;
            }
        }
    }

Upvotes: 0

Mong Zhu
Mong Zhu

Reputation: 23732

Why don't you put the logic into the getter. It seems to make more sense since you don't use value in the setter:

public float Price
{
    get
    {
        if (this.bPrice > 30)
        {
            return this.bPrice - (this.bPrice * 0.10f);
        } 
        else
        {
            return this.bPrice;
        }
    }

    private set
    {
        this.bPrice = value
    }
}

EDIT:

a short version of the getter would look like this and (thanks to Patrick Hofman) you can calculate the 90% by multiplication with 0.9:

return this.bPrice > 30? this.bPrice * 0.90f : this.bPrice;


public float Price
{
    get { return this.bPrice > 30? this.bPrice * 0.90f : this.bPrice; }

    private set { this.bPrice = value; }        
}

I made the setter private. Remove it if you want to allow the setting/manipulation of the price also after the creation of your Book object.

Upvotes: 6

Murray Foxcroft
Murray Foxcroft

Reputation: 13745

You are not using the Setter to set the price. Try the below.

 public Book(string name, string writer, string publisher, float price, string theme)
        {
            Name = name;
            writerName = writer;
            bPublisher = publisher;
            Price = price; // use the setter here, not the member variable 
            bTheme = theme;
        }

If you make the below private, then you will additional protection from people using the wrong variables

private string Name;
private string writerName;
private string bPublisher;
private float bPrice;
private string bTheme;
private float returnValue;

You should also consider making your price a decimal to avoid floating point precision errors.

And as per comment, you need to do a little more work on your property

 public float Price
        {
            get
            {
                return bPrice;
            }

            set
            {
                if (value > 30)
                {
                    bPrice = value - (value * 0.10);
                } 
                else
                {
                    bPrice = value;
                }
            }
        }

Upvotes: 3

Related Questions