473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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)

Expand|Select|Wrap|Line Numbers
  1. public static void Main()
  2.     {
  3.        CoffeeTableBook aGoodCTB = new CoffeeTableBook();
  4.  
  5.         aGoodCTB.Isbn = "9780712614894 ";
  6.         aGoodCTB.Title = "Interior Design with Feng Shui";
  7.         aGoodCTB.Author = "Sarah Rossbach";
  8.         aGoodCTB.Price = 48.75;
  9.  
  10.         Console.WriteLine("\nISBN: {0}", aGoodCTB.Isbn);
  11.         Console.WriteLine("  {0} by {1} sells for {2}",
  12.            aGoodCTB.Title, aGoodCTB.Author,
  13.            aGoodCTB.Price.ToString("C"));
  14.      }
  15. public class Book
  16. {
  17.     private string isbn;
  18.     private string title;
  19.     private string author;
  20.     protected double price;
  21.     public string Isbn { get; set; }
  22.     public string Title { get; set; }
  23.     public string Author { get; set; }
  24.     public double Price { get; set; }
  25. }
  26. public class CoffeeTableBook : Book
  27. {
  28.     public const double LOWPRICE = 35.00;
  29.     public const double HIGHPRICE = 100.00;
  30.     new public double Price
  31.     {
  32.         get
  33.         {
  34.             return base.Price;
  35.         }
  36.         set
  37.         {
  38.             if (value < LOWPRICE)
  39.                 price = LOWPRICE;
  40.             else if (value > HIGHPRICE)
  41.                 price = HIGHPRICE;
  42.             else price = value;
  43.         }
  44.     }
Attached Files
File Type: txt base.value.txt (3.8 KB, 375 views)
Jul 20 '10 #1
6 1899
GaryTexmo
1,501 Expert 1GB
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...

Expand|Select|Wrap|Line Numbers
  1. public class Person
  2. {
  3.   private string m_name;
  4.  
  5.   public string Name
  6.   {
  7.     get { return m_name; }
  8.     set { m_name = value; }
  9.   }
  10. }
... or ...

Expand|Select|Wrap|Line Numbers
  1. public class Person
  2. {
  3.   public string Name { get; set; }
  4. }
Jul 20 '10 #2
@GaryTexmo
Gary,

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

Thanks,
confounded
Jul 20 '10 #3
GaryTexmo
1,501 Expert 1GB
What don't you understand? Please post your updated code.
Jul 20 '10 #4
@GaryTexmo
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; }
}
Jul 20 '10 #5
GaryTexmo
1,501 Expert 1GB
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.
Jul 20 '10 #6
@GaryTexmo
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
Jul 21 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: modemer | last post by:
Question is as in subject. For example: class BaseClass { public: void func() { do something; } // I don't want this function being overloaded in its inherited class };
9
by: Martin Herbert Dietze | last post by:
Hello, I would like to implement a callback mechanism in which a child class registers some methods with particular signatures which would then be called in a parent class method. In...
5
by: owais | last post by:
Hi, I have a problem, I want to implements Parent class interface methods in child class. for e.g -------------- Test1.vb ---------------- Imports System Imports System.Web.UI Imports...
1
by: Peter Nofelt | last post by:
Hey All, I am having issue with serializing a class and its base class using ..net 2.0. I am using IXmlSerializable I've provided code displaying my at the bottom of this post. Thanks ahead...
4
by: mwebel | last post by:
Hi, i have another problem: i have a container basis class and want to write a virtual "copyFrom()" function. Basically the child class should copy from another child class....
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
2
by: HarisHohkl | last post by:
Hi, I've this function in a class to update the total value.but when i try to remove the these row highlight in Bold it crash, what should i do???? void display_total_value() { double...
12
by: y-man | last post by:
Hi, I am creating a child class of the vector, which contains a couple of functions to make the work with carthesian coordinate vectors easier. To make things work more easily, I would like to...
7
by: Daniel Smedegaard Buus | last post by:
Hello there :) I'm trying to implement some basic DRY MVC-like functionality as a basis to work on. Specifically, I'd like to create a base abstract model class with basic read, write, delete...
2
by: sumanta123 | last post by:
Dear Sir, In my develpment i am getting stuck for a senario.Kindly please help me for it. Everytime we give the request we get the response of 8 records and its corresponding value. Then next...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.