child class will not display the base value.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • confounded
    New Member
    • Jul 2010
    • 4

    #1

    child class will not display the base value.

    I don't understand why this program returns a value of 0 when it should return my base values. This is for the text book child and the coffee table book child.

    here is the core of the program: (the whole progam is in a txt file attached)

    Code:
    public static void Main()
        {
           CoffeeTableBook aGoodCTB = new CoffeeTableBook();
    
            aGoodCTB.Isbn = "9780712614894 ";
            aGoodCTB.Title = "Interior Design with Feng Shui";
            aGoodCTB.Author = "Sarah Rossbach";
            aGoodCTB.Price = 48.75;
    
            Console.WriteLine("\nISBN: {0}", aGoodCTB.Isbn);
            Console.WriteLine("  {0} by {1} sells for {2}",
               aGoodCTB.Title, aGoodCTB.Author,
               aGoodCTB.Price.ToString("C"));
         }
    public class Book
    {
        private string isbn;
        private string title;
        private string author;
        protected double price;
        public string Isbn { get; set; }
        public string Title { get; set; }
        public string Author { get; set; }
        public double Price { get; set; }
    }
    public class CoffeeTableBook : Book
    {
        public const double LOWPRICE = 35.00;
        public const double HIGHPRICE = 100.00;
        new public double Price
        {
            get
            {
                return base.Price;
            }
            set
            {
                if (value < LOWPRICE)
                    price = LOWPRICE;
                else if (value > HIGHPRICE)
                    price = HIGHPRICE;
                else price = value;
            }
        }
    Attached Files
    Last edited by Niheel; Jul 21 '10, 03:05 AM.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    public class Book
    {
    private string isbn;
    private string title;
    private string author;
    protected double price;
    public string Isbn { get; set; }
    public string Title { get; set; }
    public string Author { get; set; }
    public double Price { get; set; }
    }
    I've bolded your problem... which exists for all your properties actually. You've gotta be careful using the auto-implement properties :) You've actually got two properties for each item of data.

    In your CoffeeTableBook class, the property set works on "base.price " while the property get works on "base.Price ", see the difference?

    An auto-implement property doesn't magically identify a variable name for storage, it simply creates a new one. So you either need...

    Code:
    public class Person
    {
      private string m_name;
    
      public string Name
      {
        get { return m_name; }
        set { m_name = value; }
      }
    }
    ... or ...

    Code:
    public class Person
    {
      public string Name { get; set; }
    }

    Comment

    • confounded
      New Member
      • Jul 2010
      • 4

      #3
      Originally posted by GaryTexmo
      I've bolded your problem... which exists for all your properties actually. You've gotta be careful using the auto-implement properties :) You've actually got two properties for each item of data.

      In your CoffeeTableBook class, the property set works on "base.price " while the property get works on "base.Price ", see the difference?

      An auto-implement property doesn't magically identify a variable name for storage, it simply creates a new one. So you either need...

      Code:
      public class Person
      {
        private string m_name;
      
        public string Name
        {
          get { return m_name; }
          set { m_name = value; }
        }
      }
      ... or ...

      Code:
      public class Person
      {
        public string Name { get; set; }
      }
      Gary,

      Sorry I don't quite understand. I've tried to remove the multiple properties, but still can't get to function.

      Thanks,
      confounded

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        What don't you understand? Please post your updated code.

        Comment

        • confounded
          New Member
          • Jul 2010
          • 4

          #5
          Originally posted by GaryTexmo
          What don't you understand? Please post your updated code.
          I took out the other properties, but when I take out either one of the price related properties it give me an error. I apologize i've only been doing this for about a month and a half.


          public class Book
          {
          protected double price;
          public string Isbn { get; set; }
          public string Title { get; set; }
          public string Author { get; set; }
          public double Price { get; set; }
          }

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #6
            Please re-read my first post. You took out the wrong properties. You've got both a private member, price, and an auto-implemented property, Price. You either need to manually implement Price to get or set price (as demonstrated above) or you need to remove price so that the auto-implemented Price property is the only one used.

            Again, to be perfectly clear, when you use auto-implemented properties, they create internal storage that is invisible to the programmer. Therefore when your CoffeeTableBook class modifies price in the set method, but returns Price in the get method, you are not getting the same data.

            Hope that clarifies things.

            Comment

            • confounded
              New Member
              • Jul 2010
              • 4

              #7
              Originally posted by GaryTexmo
              Please re-read my first post. You took out the wrong properties. You've got both a private member, price, and an auto-implemented property, Price. You either need to manually implement Price to get or set price (as demonstrated above) or you need to remove price so that the auto-implemented Price property is the only one used.

              Again, to be perfectly clear, when you use auto-implemented properties, they create internal storage that is invisible to the programmer. Therefore when your CoffeeTableBook class modifies price in the set method, but returns Price in the get method, you are not getting the same data.

              Hope that clarifies things.
              Gary,

              Thank you alot, I did finally figure it out with this statement:

              In your CoffeeTableBook class, the property set works on "base.price" while the property get works on "base.Price", see the difference?

              Actually I needed to change the P in price in abook to lower case and it works without the protected statement!

              public string Isbn { get; set; }
              public string Title { get; set; }
              public string Author { get; set; }
              public double Price { get; set; }


              Thank you for your help!
              Confounded

              Comment

              Working...