Inheritance Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • phoenix

    #1

    Inheritance Question

    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 [MSFT]

    #2
    Re: Inheritance Question

    What is the compiler error that you're seeing?

    HabibH.

    "phoenix" <firefly@yahoo. com> wrote in message
    news:%23wGACelp DHA.2868@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    > 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
    > }
    > }
    >
    >
    >[/color]


    Comment

    • phoenix

      #3
      Re: Inheritance Question

      "Habib Heydarian [MSFT]" <habibh@microso ft.com> wrote in message
      news:O2I$SklpDH A.2500@TK2MSFTN GP10.phx.gbl...[color=blue]
      > What is the compiler error that you're seeing?
      >
      > HabibH.[/color]

      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.


      Comment

      • Matthew Reynolds

        #4
        Re: Inheritance Question

        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.

        --
        Cheers,
        Matthew
        Choosing the right web hosting company is one of the most important — and most consequential — decisions you’ll make when starting or growing your website.



        "phoenix" <firefly@yahoo. com> wrote in message
        news:#wGACelpDH A.2868@TK2MSFTN GP09.phx.gbl...[color=blue]
        > 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
        > }
        > }
        >
        >
        >[/color]


        Comment

        • Austin Ehlers

          #5
          Re: Inheritance Question

          Your code works and compiles fine on VS2003, fw1.1

          Austin Ehlers

          Comment

          Working...