calling base of base

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

    calling base of base

    How can I call the Root.Method() from B.Method()

    class Root
    {
    public Root()
    {
    }

    private virtual void Method()
    {
    }
    }

    class A : Root
    {
    public A()
    {
    }

    private virtual void Method()
    {
    base.Method();
    }
    }

    class B : A
    {
    public B()
    {
    }

    private virtual void Method()
    {
    // How to call base of base?
    base.base.Metho d(); //Error CS1041
    (this as Root).Method() ; /Error CS1540
    Root.Method(); //Error CS1540
    }

    Any idea?

    Sebastiaan.
    }


  • Jon Skeet [C# MVP]

    #2
    Re: calling base of base

    Sebastiaan Olijerhoek <no_mail@micros oft.com> wrote:[color=blue]
    > How can I call the Root.Method() from B.Method()[/color]

    You can't. For one thing you've actually defined three entirely
    separate methods in the code, because they're private. (There's no
    point in making a virtual method private.)

    Assuming you *actually* meant to make them non-private methods, with
    A.Method overriding Root.Method, and B.Method overriding A.Method, you
    still can't - you can only call "one level higher".

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Sebastiaan Olijerhoek

      #3
      Re: calling base of base

      Yes I *actually* meant that. I obviously didn't compile the code.
      Thanks.

      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
      news:MPG.1be319 50bdf75b7098b7b 4@msnews.micros oft.com...[color=blue]
      > Sebastiaan Olijerhoek <no_mail@micros oft.com> wrote:[color=green]
      > > How can I call the Root.Method() from B.Method()[/color]
      >
      > You can't. For one thing you've actually defined three entirely
      > separate methods in the code, because they're private. (There's no
      > point in making a virtual method private.)
      >
      > Assuming you *actually* meant to make them non-private methods, with
      > A.Method overriding Root.Method, and B.Method overriding A.Method, you
      > still can't - you can only call "one level higher".
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Chris Ballard

        #4
        RE: calling base of base

        Sebastiaan,

        I am not sure why you would need to call up the hierarchy like this, whilst
        missing part of the hierarchy out. It is much more common to see something
        like the following, which passes a call up the hierarchy:

        class Root
        {
        public Root()
        {
        }

        protected virtual void Method()
        {
        }
        }

        class A : Root
        {
        public A()
        {
        }

        protected virtual void Method()
        {
        base.Method();
        }
        }

        class B : A
        {
        public B()
        {
        }

        protected virtual void Method()
        {
        base.Method();
        }
        }

        Are you sure the above cannot be adapted to suit your purposes? Perhaps with
        a parameter to control which levels code executes?

        If you absolutely must call just the root method, you could add a special
        method into A, such as:
        protected void CallBaseMethod( )
        {
        base.Method();
        }
        and call that from Method() in class B. Not pretty though!

        Cheers,
        Chris.

        Comment

        • Sebastiaan Olijerhoek

          #5
          Re: calling base of base

          Chris,
          You are right it should be protected override methods.

          Your solution doesn't work for me as class A is a class in a 3rd party
          library and class Root is of the .NET framework.

          Kind regards,
          Sebastiaan.

          "Chris Ballard" <ChrisBallard@d iscussions.micr osoft.com> wrote in message
          news:EE01744D-3F92-4394-A9DF-FB43B2586891@mi crosoft.com...[color=blue]
          > Sebastiaan,
          >
          > I am not sure why you would need to call up the hierarchy like this,[/color]
          whilst[color=blue]
          > missing part of the hierarchy out. It is much more common to see something
          > like the following, which passes a call up the hierarchy:
          >
          > class Root
          > {
          > public Root()
          > {
          > }
          >
          > protected virtual void Method()
          > {
          > }
          > }
          >
          > class A : Root
          > {
          > public A()
          > {
          > }
          >
          > protected virtual void Method()
          > {
          > base.Method();
          > }
          > }
          >
          > class B : A
          > {
          > public B()
          > {
          > }
          >
          > protected virtual void Method()
          > {
          > base.Method();
          > }
          > }
          >
          > Are you sure the above cannot be adapted to suit your purposes? Perhaps[/color]
          with[color=blue]
          > a parameter to control which levels code executes?
          >
          > If you absolutely must call just the root method, you could add a special
          > method into A, such as:
          > protected void CallBaseMethod( )
          > {
          > base.Method();
          > }
          > and call that from Method() in class B. Not pretty though!
          >
          > Cheers,
          > Chris.[/color]


          Comment

          • Bonj

            #6
            Re: calling base of base

            What is Root?, what is the 3rd party library?

            "Sebastiaan Olijerhoek" <no_mail@micros oft.com> wrote in message
            news:1098456209 .361762@ernani. logica.co.uk...[color=blue]
            > Chris,
            > You are right it should be protected override methods.
            >
            > Your solution doesn't work for me as class A is a class in a 3rd party
            > library and class Root is of the .NET framework.
            >
            > Kind regards,
            > Sebastiaan.
            >
            > "Chris Ballard" <ChrisBallard@d iscussions.micr osoft.com> wrote in message
            > news:EE01744D-3F92-4394-A9DF-FB43B2586891@mi crosoft.com...[color=green]
            >> Sebastiaan,
            >>
            >> I am not sure why you would need to call up the hierarchy like this,[/color]
            > whilst[color=green]
            >> missing part of the hierarchy out. It is much more common to see
            >> something
            >> like the following, which passes a call up the hierarchy:
            >>
            >> class Root
            >> {
            >> public Root()
            >> {
            >> }
            >>
            >> protected virtual void Method()
            >> {
            >> }
            >> }
            >>
            >> class A : Root
            >> {
            >> public A()
            >> {
            >> }
            >>
            >> protected virtual void Method()
            >> {
            >> base.Method();
            >> }
            >> }
            >>
            >> class B : A
            >> {
            >> public B()
            >> {
            >> }
            >>
            >> protected virtual void Method()
            >> {
            >> base.Method();
            >> }
            >> }
            >>
            >> Are you sure the above cannot be adapted to suit your purposes? Perhaps[/color]
            > with[color=green]
            >> a parameter to control which levels code executes?
            >>
            >> If you absolutely must call just the root method, you could add a special
            >> method into A, such as:
            >> protected void CallBaseMethod( )
            >> {
            >> base.Method();
            >> }
            >> and call that from Method() in class B. Not pretty though!
            >>
            >> Cheers,
            >> Chris.[/color]
            >
            >[/color]


            Comment

            Working...