Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I using the set/get function wrong?

Tags:

c#

class

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);
}
like image 947
fibershot. Avatar asked May 15 '19 07:05

fibershot.


2 Answers

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)

like image 120
Dmitry Bychenko Avatar answered Oct 17 '22 15:10

Dmitry Bychenko


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.

like image 6
Mong Zhu Avatar answered Oct 17 '22 17:10

Mong Zhu