Inheritance Question

  • Thread starter Thread starter phoenix
  • Start date Start date
P

phoenix

I have three classes: top, middle, and bottom. The middle class inherits
the Name property of its parent, top. The bottom class inherits the Name
and ID properties of its parent, middle.

However, if I create an instance of type bottom in a different class, I am
only able to access its sole property -- Date. I don't have access to the
Name and ID properties. What am I doing wrong? I can access these
properties within the bottom code.

public class top {
private string name;

public string Name {
get { return name; }
set { name = value; }
}
}

public class middle : top {
private int id;

public int ID {
get { return id; }
set { id = value; }
}
}

public class bottom: middle {
private DateTime date;

public DateTime Date {
get { return date; }
set { date = value; }
}
}

public class stuff
{
private void DoStuff()
{
bottom bot = new bottom();
string name = bot.Name; // compile error
int id = bot.ID; // compile error
DateTime date = bot.Date; // ok
}
}
 
Habib Heydarian said:
What is the compiler error that you're seeing?

HabibH.

It says that bottom does not contain a definition for Name or ID. I have
every class in a different file and they all have the same namespace.
 
I can't see anything wrong with this.

Have you cut-n-pasted the code direct from VS .NET? The only reason why
that would not work is if you had:

bot.name

....rather than

bot.Name

....as the first one points to the (correctly) private field.
 
Back
Top