Question

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)
        {
        }
    }
}
}
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top