Inherited method

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

    Inherited method

    I got a method in my ancestor form declared as Protected, this method has
    empty body.
    In my descendant form I declared as Protected also, then compile has no
    problem but the name of the method has green underline.

    The warning of the compilation is about the method hides inherited member.
    Use the new keyword if hiding was intened.


  • Jeroen

    #2
    Re: Inherited method

    Did you perhaps intend to do:

    /////////////////////////////
    abstract class ancestor
    {
    protected virutal mymethod() {}
    }
    /////////////////////////////
    class myclass : ancestor
    {
    protected override mymethod() {}
    }
    //////////////////////////////

    -Jeroen


    Alan T wrote:
    I got a method in my ancestor form declared as Protected, this method has
    empty body.
    In my descendant form I declared as Protected also, then compile has no
    problem but the name of the method has green underline.
    >
    The warning of the compilation is about the method hides inherited member.
    Use the new keyword if hiding was intened.

    Comment

    • Mark R. Dawson

      #3
      RE: Inherited method

      Hi Alan,
      basically what the compiler is telling you that a method in your derived
      class is hiding a method in the base class. It is possible you want to do
      this, if so you want to add the new keyword to the method signature in the
      derived class which explicitly states you know you are hiding a method and
      intended to do so. So for example:

      class Person
      {
      public string Description()
      {
      return "I am a person";
      }
      }

      class Man : Person
      {
      public string Description()
      {
      return "I am a man";
      }
      }

      class Program
      {
      static void Main(string[] args)
      {
      Man m = new Man();

      //Outputs "I am a man"
      Console.Out.Wri teLine(m.Descri ption());

      Person p = m;

      //Outputs "I am a person"
      Console.Out.Wri teLine(p.Descri ption());
      }
      }

      The method called on the variable is defined by it's runtime type, so you
      can see the description method in the Man class hides the implementation in
      the base Person class when accessing it with a reference of type man. If you
      want to get rid of the compiler warning you can use the new keyword i.e.:

      public new string Description()

      If you want to actually change the implementation of the method in the
      derived class then you need to make the method virtual in the base class and
      then override the method in the derived class.

      Mark.
      --



      "Alan T" wrote:
      I got a method in my ancestor form declared as Protected, this method has
      empty body.
      In my descendant form I declared as Protected also, then compile has no
      problem but the name of the method has green underline.
      >
      The warning of the compilation is about the method hides inherited member.
      Use the new keyword if hiding was intened.
      >
      >
      >

      Comment

      • Mark R. Dawson

        #4
        RE: Inherited method

        The method called on the variable is defined by it's runtime type, so you
        can see the description method in the Man class hides the implementation in
        the base Person class when accessing it with a reference of type man
        should be:

        The method called on the variable is defined by it's runtime type, so you
        can see the description method in the Man class hides the implementation in
        the base Person class when accessing it with a reference of type PERSON
        --



        "Mark R. Dawson" wrote:
        Hi Alan,
        basically what the compiler is telling you that a method in your derived
        class is hiding a method in the base class. It is possible you want to do
        this, if so you want to add the new keyword to the method signature in the
        derived class which explicitly states you know you are hiding a method and
        intended to do so. So for example:
        >
        class Person
        {
        public string Description()
        {
        return "I am a person";
        }
        }
        >
        class Man : Person
        {
        public string Description()
        {
        return "I am a man";
        }
        }
        >
        class Program
        {
        static void Main(string[] args)
        {
        Man m = new Man();
        >
        //Outputs "I am a man"
        Console.Out.Wri teLine(m.Descri ption());
        >
        Person p = m;
        >
        //Outputs "I am a person"
        Console.Out.Wri teLine(p.Descri ption());
        }
        }
        >
        The method called on the variable is defined by it's runtime type, so you
        can see the description method in the Man class hides the implementation in
        the base Person class when accessing it with a reference of type man. If you
        want to get rid of the compiler warning you can use the new keyword i.e.:
        >
        public new string Description()
        >
        If you want to actually change the implementation of the method in the
        derived class then you need to make the method virtual in the base class and
        then override the method in the derived class.
        >
        Mark.
        --

        >
        >
        "Alan T" wrote:
        >
        I got a method in my ancestor form declared as Protected, this method has
        empty body.
        In my descendant form I declared as Protected also, then compile has no
        problem but the name of the method has green underline.

        The warning of the compilation is about the method hides inherited member.
        Use the new keyword if hiding was intened.

        Comment

        • Ben Voigt

          #5
          Re: Inherited method


          "Mark R. Dawson" <MarkRDawson@di scussions.micro soft.comwrote in message
          news:3DAED33B-4D11-45EF-BC42-2AD352DD281A@mi crosoft.com...
          >The method called on the variable is defined by it's runtime type, so you
          >can see the description method in the Man class hides the implementation
          >in
          >the base Person class when accessing it with a reference of type man
          >
          should be:
          >
          The method called on the variable is defined by it's runtime type, so you
          can see the description method in the Man class hides the implementation
          in
          the base Person class when accessing it with a reference of type PERSON
          --
          That's only true for virtual methods, so you were right the first time.


          Comment

          Working...