1

I am working though learning C# on my own (not homework). Not sure why my "public new double Price" is being skipped over in my TextBook and CoffeeTableBook child classes. I think it is due to my constructor as that is the last line of code that executes before it exits the class. Haven't learned anything too fancy yet, looking for simplicity. Thank you.

namespace BookDemo
{
class Program
{
    static void Main()
    {
        Book book1 = new Book (123, "BOOK: The Blue Whale", "Liam Smith", 15.99);
        Book book2 = new Book(456, "BOOK: The Blue Whale 2", "Liam Smith", 35.00);
        TextBook book3 = new TextBook(789, "TEXTBOOK: Math 101", "Bob Taylor", 1000.00, 10);
        CoffeeTableBook book4 = new CoffeeTableBook(789, "TEXTBOOK: Math 101", "Molly Burns", 0.10);

        Console.WriteLine(book1.ToString());
        Console.WriteLine(book2.ToString());
        Console.WriteLine(book3.ToString());
        Console.WriteLine(book4.ToString());
        Console.ReadLine();
    }

    class Book
    {
        private int Isbn { get; set; }
        private string Title  { get; set; }
        private string Author { get; set; }
        protected double Price { get; set; }

         //Book Constructor
        public Book(int isbn, string title, string author, double price)
        {
            Isbn = isbn;
            Title = title;
            Author = author;
            Price = price;
        }

        public override string ToString()
        {
            return("\n" + GetType() + "\nISBN: " + Isbn + "\nTitle: " + Title + "\nAuthor: " + Author + "\nPrice: " + Price.ToString("C2"));
        }
    }

    class TextBook : Book
    {
        private const int MIN = 20;
        private const int MAX = 80;
        private int GradeLevel { get; set; }

        //TextBook Constructor
        public TextBook(int isbn, string title, string author, double price, int grade) : base (isbn, title, author, price)
        {
            GradeLevel = grade;
        }

        public new double Price
        {
            set
            {
                if (value <= MIN)
                    Price = MIN;
                if (value >= MAX)
                    Price = MAX;
                else
                    Price = value;
            }
        }    
    }

    class CoffeeTableBook : Book
    {
        const int MIN = 35;
        const int MAX = 100;

        public new double Price  // min 35, max 100
        {                
            set
            {
                if (value <= MIN)
                    Price = MIN;
                if (value >= MAX)
                    Price = MAX;
                else
                    Price = value;
            }
        }

        //CoffeeTable Book Constructor
        public CoffeeTableBook(int isbn, string title, string author, double price) : base (isbn, title, author, price)
        {
        }
    }
}
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
JLT
  • 67
  • 1
  • 9
  • Not sure what behavior you observe, but likely you are looking for `virtual`/`override` rather than `new`. Check out [confused about new vs. virtual](http://stackoverflow.com/questions/2952887/confused-about-override-vs-new-in-c-sharp) – Alexei Levenkov May 04 '14 at 00:36
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackoverflow.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 04 '14 at 00:52

1 Answers1

4

The new keyword hides the original property. Because your Book class has a protected Price property, and the TextBook class has a public property, the compiler is recognizing them as 2 different properties, the one in the TextBook class as a completely new and unrelated property. When accessing the Price, you will still end up with the price from the base class (Book)

Try using virtual and override instead, as well as keeping the access modifiers consistent (They should both be public)

class Book
{
    ...
    public virtual double Price { get { return price; } set { price = value; } } //Property
    protected double price; //Backing Field
    ...
}
class TextBook : Book
{
    ...
    public override double Price
    {
        set
        {
            if (value <= MIN)
                price = MIN;
            if (value >= MAX)
                price = MAX;
            else
                price = value;
        }
    }    
    ...
}

See more on Override/Virtual vs New on another question. Specifically from it, this diagram in the answers:

https://farm4.static.flickr.com/3291/2906020424_f11f257afa.jpg?v=0

Cyral
  • 13,999
  • 6
  • 50
  • 90
  • Thanks for your response. That's what I originally had but it gives me a stack overflow. Also, shouldn't the Price in the parent Book class be Protected? Thx. – JLT May 04 '14 at 00:39
  • It is worth commenting that the code uses the property as defined in `Book` because it is assigned in the `Book` constructor. – SJuan76 May 04 '14 at 00:40
  • @JLT, have a look at my edit, when using properties to handle logic (Since your `TextBook` property is not an auto-property anymore) you need to use a private or protected field to store the value in, and a property to access it. (Because setting the Price, will call `set`, and set it again, in an infinite loop) – Cyral May 04 '14 at 00:42
  • Thanks, Cyral. That makes sense and did the trick! My MAX prices are working, but when I change the price to below MIN, it isn't setting it to the MIN value. I also changed the int to double for the child class consts. I am hoping that I am using the constructors properly, seems repetitive. Thank you. – JLT May 04 '14 at 00:56
  • That should work, I'm not sure why it wouldn't be. If you are worried about repetitive code, move the `MIN, `MAX`, and `Price` (With the logic) to your `Book` class. Make sure the `MIN` and `MAX` are no longer `const`. Make them `readonly` and assign them in the constructors in each of the inheriting classes. – Cyral May 04 '14 at 01:00
  • @Cyral Anyway to PM you? No chatroom here. Thanks. – JLT May 04 '14 at 01:22
  • Figured out why the MIN isn't working - I needed to add an else after the first if... – JLT May 04 '14 at 02:09
  • Ah how did I miss that, glad you fixed it though – Cyral May 04 '14 at 02:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51953/discussion-between-jlt-and-cyral) – JLT May 04 '14 at 02:43